Ron Natalie wrote:
Pete Becker wrote: Bi****@abv.bg wrote:
Is it safe to call nonvirtual member functions from ctors and
dtors? What about virtual ones?
Yes, it's safe. Unlike Java, C++ has a safe rule for virtual
functions: when a constructor or destructor calls a virtual function it calls
the function defined for the type whose constructor or destructor is
currently being run, which isn't necessarily the most derived type.
With the exception of virtual calls to pure virtual functions in the
constructors (these are undefined).
And destructors, 10.4/6, and it also includes indirect calls.
The reason is that pure virtual functions don't have to appear in
vtables.
The vtable entries will be overwritten by the derived constructor, as
every pure virtual function must be defined later, in a derived class.
Non-virtual calls don't use vtables, and therefore are defined.
(Implementations which don't use vtables are still bound by the same
rules. )
HTH,
Michiel Salters