Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a pure virtual function initialized by 0?

People also ask

What does a virtual function 0 mean?

The = 0 after a virtual function means that "this is pure virtual function, it must be implemented in the derived function". As in your example, it's still possible to implement the function. It has no other meaning as such - and you can't use other numbers, addresses or anything else.

Which is used to create a pure virtual function & 0?

You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.

What is a pure virtual function?

A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly.

What happens when a pure virtual function is called?

Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call (10.3) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined.


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.


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 keyword pure or abstract because at the time I saw no chance of getting a new keyword accepted. Had I suggested pure, 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 over pure, 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


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.


I'm not sure if there is any meaning behind this. It is just the syntax of the language.


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.