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

风流意气都作罢 提交于 2020-01-31 02:21:30

问题


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)  -->> here ?
{
  -->> here ?
  //...
  -->> here ?
}

回答1:


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.




回答2:


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..)




回答3:


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 "


来源:https://stackoverflow.com/questions/6591859/when-does-the-vptr-pointing-to-vtable-get-initialized-for-a-polymorphic-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!