If you want to bypass the virtual dispatch and call the body of the function that you defined, you must qualify the function name:
virtual ~A() { A::f(); }
Otherwise, the call initiates a virtual dispatch, but only to the base class, because the object of the derived type is already destroyed before its bases.
C ++ 11 §12.7 / 4 directly solves your question:
Member functions, including virtual functions (10.3), can be called during construction or destruction (12.6.2). When a virtual function is called directly or indirectly from a constructor or from a destructor, including when constructing or destroying non-static elements of these classes, and the object to which the call is applied is the object (name it x) for construction or destruction, the called function is the final redefinition in the class of constructors or destructors and does not override it in a more derived class. If the call to a virtual function uses explicit access to a class member (5.2.5), and the expression of the object refers to the full object x or one of the subobjects of the base class of objects, but not to x or one of its subobjects of the base class, the behavior is undefined.
However, §10.4 / 6 forbids this to be done with a pure virtual function:
Member functions can be called from the constructor (or destructor) of an abstract class; the effect of a virtual call (10.3) on a pure virtual function, directly or indirectly, for a created (or destroyed) object from such a constructor (or destructor) is undefined.
So this is UB.
The effect of “pure virtual” is to hide the definition of a function from virtual search. You will never achieve the definition of a pure virtual function from a dynamic dispatch call, except possibly as an undefined behavior effect.
source share