When a virtual function is called directly or indirectly from a constructor
or from a destructor,
including during the construction or destruction of the class's non-static data
members,
or during the evaluation of
a postcondition assertion of a constructor or
a precondition assertion of a destructor (
[dcl.contract.func]),
and the object to which the call applies is the object (call it
x) under construction or
destruction,
the function called is the
final overrider in the constructor's or destructor's class and not one
overriding it in a more-derived class
. If the virtual function call uses an explicit class member access (
[expr.ref])
and the object expression refers to
the complete object of
x or one of that object's base class subobjects
but not
x or one of its base class subobjects, the behavior
is undefined
. [
Example 4:
struct V {
virtual void f();
virtual void g();
};
struct A : virtual V {
virtual void f();
};
struct B : virtual V {
virtual void g();
B(V*, A*);
};
struct D : A, B {
virtual void f();
virtual void g();
D() : B((A*)this, this) { }
};
B::B(V* v, A* a) {
f();
g();
v->g();
a->f();
}
—
end example]