Introduction to virtual function table

Polymorphism is implemented by virtual functions, and virtual functions are mainly implemented through virtual function tables (V-Tables).

If a class contains virtual functions (virtual decorated functions), then the class will contain a virtual function table, and each item stored in the virtual function table is the address of a virtual function. As shown below:

write picture description here

Each object of this class will contain a virtual pointer (the virtual pointer exists at the front of the address of the object instance to ensure the highest performance of the virtual function table), and this virtual pointer points to the virtual function table.

Note: The object does not contain the virtual function table, only the virtual pointer, the class contains the virtual function table, the derived class will generate a virtual function table compatible with the base class.

 The virtual function table of the original base class:  The
  following figure is the object of the original base class. You can see that the virtual pointer is at the top of the address, pointing to the virtual function table of the base class (assuming that the base class defines 3 virtual functions) 
write picture description here

·  Virtual functions in single inheritance (without overriding base class virtual functions) 
Assuming that the derived class inherits the base class and redefines 3 virtual functions (which are the derived class's own virtual functions), the derived class will generate a compatible base by itself The virtual function table of the class is its own virtual function table. 
write picture description here

The Derive class inherits the three virtual functions in the Base class. To be precise, the address of the function entity is copied to the virtual function table of the Derive class. The virtual functions added by the derived class are placed behind the virtual function table, and press Declarations are stored in order.

·  Virtual function in single inheritance (overriding base class virtual function) 
Now the derived class rewrites the x function of the base class, you can see that when the derived class builds its own virtual function table, it modifies the base::x() An item that points to its own virtual function. 
write picture description here

·  Virtual function when multiple inheritance (Derived :: public Base1, public Base2) 
This derived class multiple inherits two base classes base1, base2, so it has two virtual function tables. 
write picture description here 
  

  Its object will have multiple virtual pointers (it is said to be related to the compiler), pointing to different virtual function tables.

  Adjustment of pointers during multiple inheritance:

Derive b;
Base1* ptr1 = &b;   // 指向 b 的初始地址
Base2* ptr2 = &b;   // 指向 b 的第二个子对象

Because Base1 is the first base class, ptr1 points to the starting address of the Derive object, and no pointer adjustment (offset) is required.

Because Base2 is the second base class, the pointer must be adjusted by adding an offset so that ptr2 points to the Base2 subobject.

Of course, the above process is done by the compiler.

Base1* b1 = (Base1*)ptr2;
b1->y();                   // 输出 Base2::y()
Base2* b2 = (Base2*)ptr1;
b2->y();                   // 输出 Base1::y()

In fact, when accessing a member through a pointer of a certain type, the compiler just finds the offset of the member according to the type definition, and uses this offset to obtain the member. 
Since ptr2 originally points to the starting address of the Base2 sub-object, b1->y() calls Base2::y(), and ptr1 originally points to the starting address of the Base1 sub-object (that is, the starting address of the Derive object). address), so b2->y() calls Base1::y().

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324357963&siteId=291194637
Recommended