Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ constructors: why is this virtual function call not safe?
    primarykey
    data
    text
    <p>This is from the C++11 standard sec 12.7.4. This is rather confusing.</p> <ol> <li>What does the last sentence in the text mean exactly?</li> <li>Why is the last method call in <code>B::B</code> undefined? Shoudn't it just call <code>a.A::f</code>? </li> </ol> <blockquote> <p>4 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 during the construction or destruction of the class’s non-static data members, 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 (5.2.5) 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:</p> <pre><code>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(); // calls V::f, not A::f g(); // calls B::g, not D::g v-&gt;g(); // v is base of B, the call is well-defined, calls B::g a-&gt;f(); // undefined behavior, a’s type not a base of B } </code></pre> <p>—end example ]</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload