Virtual function table and virtual table pointer

1. The meaning of virtual function

The member function of a class declared with virtual is called a virtual function

2. Function

Used to achieve polymorphism

  1. There is an inheritance relationship, the child class inherits the parent class
  2. The subclass rewrites the virtual function of the parent class
  3. The subclass appears as a pointer or reference to the parent class

3. The realization principle of virtual function

The key is two points:

  • Virtual function table pointer
  • Virtual function table

To understand the structure, we still have to look at the memory layout

The following is to use the debug method to view the layout size

Be sure to understand the following four pieces of code


1. The memory layout of ordinary classes

Code:

Because ordinary functions do not occupy memory, so the object a does not display memory at this time


2. Memory layout of virtual function class

Code:

At this time, because the virtual function is defined in the class, there is a virtual function pointer: _vfptr

This function pointer points to the virtual function table, and the table contains the addresses of two virtual functions


3. The subclass does not rewrite the memory layout of the virtual function of the parent class

Code:

At this time, the subclass inherits the virtual function of the parent class, but it is not overwritten. Therefore, the virtual function of the parent class is still stored in the table pointed to by the virtual function pointer of the subclass. Therefore, you can also see that the addresses inside are the same


4. The subclass rewrites the memory layout of the virtual function of the parent class

Code:

In this local variable, you can see that the address in the virtual function table of the rewritten function has changed, and the address in the virtual function table of the function that is not rewritten remains unchanged.

If you understand the above four pictures, you will probably know the relationship between the virtual function table pointer and the virtual function table.

That is, when there is a virtual function in the class, a virtual function table pointer is automatically generated, this pointer points to a virtual function table, and the virtual function defined in the class is stored in the table

to sum up:

1. Virtual function table pointer

  • What is a virtual function table pointer, where is it, and what is it for?

       We call the 4 bytes or 8 bytes from the first address of the object, this position is called the virtual function table pointer (you can add a point attribute to see the position). It contains an address that points to the address of the virtual function table

2. Virtual function table

  • What is a virtual function table, where is it, and what is it for

        The virtual function table is an array of addresses (function pointer array). Its location is the address stored in the virtual function table pointer. The address contained in it is the address of the virtual function that we overwrite the parent class ( Without overriding the virtual function of the parent class, the default is the function address of the parent class)

Guess you like

Origin blog.csdn.net/qq_46423166/article/details/112297810