In C++, when a virtual function is called from a constructor or destructor during construction or destruction, the final overridden function in the same class is called and not the overridden function in any child class.
class A {
virtual void f();
A() {
f(); // Always calls A::f(), even when an object of B is being constructed
}
};
class B : public A {
void f() override;
};
This is because when a class is being constructed, the child class is not constructed yet and it won’t be safe to call any of its functions. Similarly, when a class is destructed, its child classes have already been destructed.