Reputation: 2778
We always declare a pure virtual function as:
virtual void fun () = 0 ;
I.e., it is always assigned to 0.
What I understand is that this is to initialize the vtable entry for this function to NULL and any other value here results in a compile time error. Is this understanding correct or not?
Upvotes: 173
Views: 60572
Reputation:
The reason =0
is used is that Bjarne Stroustrup didn't think he could get another keyword, such as "pure" past the C++ community at the time the feature was being implemented. This is described in his book, The Design & Evolution of C++, section 13.2.3:
The curious =0 syntax was chosen ... because at the time I saw no chance of getting a new keyword accepted.
He also states explicitly that this need not set the vtable entry to NULL, and that doing so is not the best way of implementing pure virtual functions.
Upvotes: 181
Reputation: 224079
C++ has always shied away from introducing new keywords, since new reserved words break old programs which use these words for identifiers. It's often seen as one of the language's strengths that it respects old code as far as possible.
The = 0
syntax might indeed have been chosen since it resembles setting a vtable entry to 0
, but this is purely symbolic. (Most compilers assign such vtable entries to a stub which emits an error before aborting the program.) The syntax was mainly chosen because it wasn't used for anything before and it saved introducing a new keyword.
Upvotes: 17
Reputation: 1347
I'm not sure if there is any meaning behind this. It is just the syntax of the language.
Upvotes: 17
Reputation: 82535
Section 9.2 of the C++ standard gives the syntax for class members. It includes this production:
pure-specifier:
= 0
There is nothing special about the value. "= 0" is just the syntax for saying "this function is pure virtual." It has nothing to do with initialization or null pointers or the numeric value zero, although the similarity to those things may have mnemonic value.
Upvotes: 34
Reputation: 490178
As with most "Why" questions about the design of C++, the first place to look is The Design and Evolution of C++, by Bjarne Stroustrup1:
The curious
=0
syntax was chosen over the obvious alternative of introducing a new keywordpure
orabstract
because at the time I saw no chance of getting a new keyword accepted. Had I suggestedpure
, Release 2.0 would have shipped without abstract classes. Given a choice between a nicer syntax and abstract classes, I chose abstract classes. Rather than risking delay and incurring the certain fights overpure
, I used the tradition C and C++ convention of using 0 to represent "not there." The=0
syntax fits with my view that a function body is the initializer for a function also with the (simplistic, but usually adequate) view of the set of virtual functions being implemented as a vector of function pointers. [ ... ]
1§13.2.3 Syntax
Upvotes: 87
Reputation: 46617
The = 0
declares a pure virtual function.
What is understand is that this is to initialize the vtable entry for this function to NULL and any other value here results in compile time error
I don't think that's true. It's just special syntax. The vtable is implementation-defined. No one says a vtable entry for a pure member must be actually zeroed upon construction (although most compilers handle vtables similar).
Upvotes: 2
Reputation: 25834
Well, you can also initialize the vtable entry to point to an actual function"
virtual void fun()
{
//dostuff()
}
Seems intuitive that the vtable entry can either be defined to point nowhere (0) or to a function. Letting you specify your own value for it would probably result in it pointing to garbage instead of to a function. But that is why "= 0" is allowed and "= 1" is not. I suspect Neil Butterworth is right about why "= 0" is used at all
Upvotes: 1
Reputation: 37463
I remember reading that the justification for the funny syntax was that it was easier (in terms of standards-acceptance) than introducing another keyword that would do the same thing.
I believe this was mentioned in The Design and Evolution of C++ by Bjarne Stroustrup.
Upvotes: 3
Reputation: 320541
Nothing is "initilaized" or "assigned" zero in this case. = 0
in just a syntactical construct consisting of =
and 0
tokens, which has absolutely no relation to either initialization or assignment.
It has no relation to any actual value in "vtable". The C++ language has no notion of "vtable" or anythng like that. Various "vtables" are nothing more than just details of specific implementations.
Upvotes: 7
Reputation: 754833
C++ must have a way to distinguish a pure virtual function from a declaration of a normal virtual function. They chose to use the = 0
syntax. They could just have easily done the same by adding a pure keyword. But C++ is pretty loath to add new keywords and prefers to use other mechanisms to introduce features.
Upvotes: 11
Reputation: 11284
I would assume that this is just part of the C++ grammar. I don't think there are any restrictions to how the compilers actually implement this for a given specific binary format. You're assumption probably was right for the early day C++ compilers.
Upvotes: 2