For a derived class to override a function, the function must be declared virtual in the base class. This means that the call function is selected at runtime when the function is called, according to the dynamic type of the object.
An alternative to overriding if the derived type of each object is known at compile time is to use the so-called “curiously repeating template pattern” (CRTP) to bring the knowledge of the derived type to the base class (which should become a template to support this):
template <typename Derived> class A { public: void foo() { static_cast<Derived*>(this)->print(); } }; class B : public A<B> { private: friend class A<B>;
Mike seymour
source share