Contents Up << >>

When my base class's constructor calls a virtual function, why doesn't my derived class's virtual function get invoked?

During the Base class's constructor, the object isn't yet a Derived, so if "Base::Base()" calls a virtual function "virt()", the "Base::virt()" will be invoked, even if "Derived::virt()" exists.

Similarly, during Base's destructor, the object is no longer a Derived, so when Base:: Base() calls "virt()", "Base::virt()" gets control, not the "Derived::virt()" override.

You'll quickly see the wisdom of this approach when you imagine the disaster if "Derived::virt()" touched a member object from the Derived class.