1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
Pure virtual member functions may be implemented. To implement a pure virtual
i(member function: pure virtual and implemented)
hi(implementing pure virtual member functions)
hi(pure virtual functions: implementing)
member function, provide it with its normal tt(= 0;) specification, but
implement it nonetheless. Since the tt(= 0;) ends in a semicolon, the pure
virtual member is always at most a declaration in its class, but an
implementation may either be provided in-line below the class interface or it
may be defined as a non-inline member function in a source file of its own.
Pure virtual member functions may be called from derived class objects or
from its class or derived class members by specifying the base class and scope
resolution operator with the function to be called. The following small
program shows some examples:
verbinclude(polymorphism/examples/purevirtual.cc)
Implementing a pure virtual function has limited use. One could argue that
the pure virtual function's implementation may be used to perform tasks that
can already be performed at the base-class level. However, there is no
guarantee that the base class virtual function will actually be called from
the derived class overridden version of the member function (like
a base class constructor that is automatically called from a derived class
constructor). Since the base class implementation will therefore at most be
called optionally its functionality could as well be implemented in a separate
member, which can then be called without the requirement to mention the base
class explicitly.
|