C++ virtual function table and virtual function table pointer creation timing

1. Timing of creating virtual function table

  • The compiler finds that the class contains functions modified with the virtual keyword

  • The contents of the virtual function table have been generated by the compiler during compilation.

  • The virtual function table is stored in the read-only data segment in the global data area.

  • The virtual function table is an array that stores the addresses of imaginary numbers.

2. Creation timing of virtual function table pointer

  • When the class object is constructed, in the constructor, assign the address of the virtual function table to the object vptr

  • If the class has no constructor, the compiler generates a default constructor for the class, thereby initializing the vptr for the class object

  • Under inheritance, the virtual function table pointer assignment process
    . When calling the base class constructor, first assign the base class's virtual function table address to vptr.
    Then when calling the subclass constructor, assign the subclass's virtual function table address to vptr.


I recommend a Lingsheng Academy project class. I personally think the teacher taught it well. I would like to share it with you:
Lingsheng Platinum Learning Card (including infrastructure/high-performance storage/golang cloud native/audio and video/Linux kernel)
https://xxetb.xet .tech/s/VsFMs

Guess you like

Origin blog.csdn.net/qq_40135848/article/details/132922727