Re: Implementation of abstract classes
Rune Allnor wrote:
Can this be correct? The way I understand
virtual void foo() = 0;
is that the statement inituializes a NULL
pointer in the virtaul function table.
You understand wrongly. The "=0" part is nothing more than a way of
saying "pure". (The story goes that Stroustrup wanted to use the keyword
"pure", but the standardization committee didn't want any more
single-use keywords, so Stroustrup came up with the next best thing he
could come up with, which was adding "=0" to the method declaration.)
A pure virtual function is in all ways a regular virtual function,
with the exception that it *must* be implemented in a derived class, and
if there's a pure virtual function in the base class, that base class
cannot be instantiated all by itself. The difference between a virtual
function and a pure virtual function is purely semantical.
This means that a pure virtual function can have an implementation in
the exact same way as a regular virtual function can. You can call this
implementation by calling it explicitly, eg. Base::foo();
In the case of a pure virtual destructor, the destructor of the base
class is called automatically (so it doesn't need to be called explicitly).