简体   繁体   English

C ++ - 与对象或类关联的vptr和vtbl?

[英]C++ — vptr & vtbl associated with object or class?

vptr -- virtual table pointer vptr - 虚拟表指针

vtbl -- virtual table vtbl - 虚拟表

Question 1> Is it correct that vptr is associated with the object of a class? 问题1> vptr与类的对象关联是否正确?

Question 2> Is it correct that vtbl is associated with the class? 问题2> vtbl与班级相关联是否正确?

Question 3> How do they really work together? 问题3>他们如何真正合作?

Thank you 谢谢

Note that vptr and vtbl are Implementation defined the C++ standard does not even talk about them. 请注意, vptrvtbl是实现定义的C ++标准甚至没有谈论它们。 However, most known compilers implement dynamic dispatch through vptr and vtbl . 但是,大多数已知编译器通过vptrvtbl实现动态调度。

Question 1: Is it correct that vptr is associated with the object of a class? 问题1: vptr与类的对象关联是否正确?

YES
vptr is associated with the object of a Class which contains Atleast one virtual member function. vptr与包含Atleast one虚拟成员函数的Class的对象相关联。 The compiler adds Vptr to every object of the Class which is Polymorphic (contains atleast one virtual member function)The first 4 bytes of the this pointer then point to the vptr 编译器将Vptr添加到Class的每个对象,它是Polymorphic (包含至少一个虚拟成员函数)。 this指针的前4个字节然后指向vptr

Question 2: Is it correct that vtbl is associated with the class? 问题2: vtbl与班级相关联是否正确?

YES
Vtbl is associated with a Class. Vtbl与Class相关联。 A vtbl gets created for each Polymorphic class. 为每个Polymorphic类创建一个vtbl

Question 3: How do they really work together? 问题3:他们如何真正合作?

The compiler adds a vptr to every object of a Polymorphic class and also creates a vtbl for each of that class. 编译器将vptr添加到Polymorphic类的每个对象,并为每个类创建一个vtbl The vptr points to the vtbl . vptr指向vtbl The vtbl contains a list of addresses of all the virtual functions in that class. vtbl包含该类中所有虚函数的地址列表。 In case if a derived class overrides a virtual function of the Base Class then the address entry for that particular function in the vtbl is replaced by the address of the overridden function. 如果派生类重写基类的虚函数,则vtbl该特定函数的地址条目将被重写函数的地址替换。 At runtime the compiler traverses the vtbl of the particular class(Base or Derived) based on the address inside the pointer rather than the type of pointer and calls the function address in the vtbl . 在运行时,编译器根据指针内的地址而不是指针类型遍历特定类(Base或Derived)的vtbl ,并调用vtbl的函数地址。 Thus Dynamic Polymorphism is achieved. 因此实现了动态多态性
The cost of this dynamic polymorphism is: 这种动态多态的成本是:
fetch (fetch the vptr inside this) fetch (fetch the function address from list of functions in vtbl) Call (Call the function) fetch (获取其中的vptr) fetch (从vtbl中的函数列表中获取函数地址) Call (调用函数)

as against call (direct call to function since static binding). 反对call (自静态绑定以来直接调用函数)。

The virtual table pointer is just a pointer inside every object of your class, pointing to the correct virtual table. 虚拟表指针只是类中每个对象的指针,指向正确的虚拟表。 Inside the virtual table are the addresses for the virtual functions of your class. 虚拟表内部是您的类的虚函数的地址。 The compiler traverses the virtual table to get the correct function when invoking a function through a base class pointer. 当通过基类指针调用函数时,编译器遍历虚拟表以获取正确的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM