开发者

When does the vptr (pointing to vtable) get initialized for a polymorphic class?

This is not about "When VTABLE is created?". Rather, when the VPTR should be initialized? Is it at the beginning/end of the constructor or before/after the constructor?

A::A () : i(0), j(0)开发者_如何学Go  -->> here ?
{
  -->> here ?
  //...
  -->> here ?
}


The machinery for virtual calls (usually a v-table, but doesn't need to be) is set up during the ctor-initializer, after construction of base subobjects and before construction of members. Section [class.base.init] decrees:

Member functions (including virtual member functions, 10.3) can be called for an object under construction. Similarly, an object under construction can be the operand of the typeid operator (5.2.8) or of a dynamic_cast (5.2.7). However, if these operations are performed in a ctor-initializer (or in a function called directly or indirectly from a ctor-initializer) before all the mem-initializers for base classes have completed, the result of the operation is undefined.

Actually, during construction of the base subobjects, the virtual function machinery exists, but it is set up for the base class. Section [class.cdtor] says:

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.


it's initialized between the constructors of base and derived classes:

class Base { Base() { } virtual void f(); };
class Derived { Derived(); virtual void f(); };

It happens when raw memory is converted to Base object. It happens when Base object is converted to Derived object during construction of an object. Same obviously happens in reverse when destroying object. I.e. every time when type changes, the vtable pointer is changed. (I'm sure someone comments that vtables don't need to exists according to the std..)


This msdn article explains it in great detali

There it says :

"And the final answer is... as you'd expect. It happens in the constructor."

so..
A::A () : i(0), j(0)
{
-->> here !
//...
//
}

But be careful, let's say you have the class A, and a class A1 derived from A.

  • If you create a new A object, the vptr will be set right at the beginning of the constructor of the A class
  • But if you were to create a new object A1:

"Here's the entire sequence of events when you construct an instance of class A1:

  1. A1::A1 calls A::A
  2. A::A sets vtable to A's vtable
  3. A::A executes and returns
  4. A1::A1 sets vtable to A1's vtable
  5. A1::A1 executes and returns "
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜