12

Possible Duplicate:
Why pure virtual function is initialized by 0?

I know that, in order to declare a pure virtual function you need to use "= 0;" syntax, like so:

class Foo  
{
protected:
    Foo();
    virtual int getValue() = 0;
};

My question is, what exactly (in the internal workings of the compiler) does the "= 0;" syntax do? Does it actually set the function pointer equal to zero? Does it serve as nothing more than a statement of intent, like the "abstract" reserved word in Java and C#, and if so, why not add a reserved word such as "abstract" to the language rather than using such arcane syntax?

Community
  • 1
  • 1
Sepulchritude
  • 183
  • 3
  • 10
  • This question is answered [here](http://stackoverflow.com/questions/2156634/why-pure-virtual-function-is-initialized-by-0). – Nerdtron Feb 14 '12 at 15:40
  • 7
    Because the `pure` keyword would be too readable for C++ programmers. – orlp Feb 14 '12 at 15:44
  • @nightcracker: Bjarne Stroustrup is wary of contextual keywords (ie, identifiers that are only keywords in some contexts and not others) and rightly so because it makes lexing a bit more difficult (ie you cannot say whether a particular identifier is a keyword or not without its context). I cannot blame it for this. However he is also wary about introducing full keywords, and this yields all those weird usages (`static` !!!!). – Matthieu M. Feb 14 '12 at 16:08
  • @MatthieuM. C++11 does have some contextual keywords, though :) – fredoverflow Feb 14 '12 at 16:14
  • Syntax has no inherent meaning. Just because two syntactic constructs look somewhat similar does *not* imply that they also have similar semantics. – fredoverflow Feb 14 '12 at 16:15
  • @FredOverflow: yes "for the sake of backward compatibility". Having seen (and attempted to understand) the lexer code in Clang, with its weird plugs for semantic disambiguation, I can say it is a mess :x – Matthieu M. Feb 14 '12 at 16:26

3 Answers3

1

It declares a 'pure virtual' function. The = 0 is basically like another 'pure' keyword. This question is related to yours: Why is a pure virtual function initialized by 0?

A pure virtual function has no body at all and must be defined by any classes which inherit it: http://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/

Community
  • 1
  • 1
David
  • 322
  • 1
  • 3
0

That signifies that there is no "default" implementation. Any derived class needs to implement it.

That Chuck Guy
  • 443
  • 4
  • 12
0

It forces you to define it in a child class.

Dmitriy Kachko
  • 2,804
  • 1
  • 19
  • 21