Although many people doubt C ++ class objects, their usage continues to increase year after year. For example, if you want to fully understand C ++ class objects, you must first understand what C ++ class objects are and how they operate.
First, we will introduce the memory layout of class objects with inheritance relationships in C ++: In C ++, if the class contains virtual functions, then it will have a virtual function table pointer _ vfptr, in the class object's initial memory data. It is followed by the memory data of the member variables in the class.
For child classes, the initial memory data records copies of parent class objects, including the parent class virtual function table pointer and member variables ). The member variable data of the Child class. The same principle applies to subclass. However, no matter how many sub-classes are inherited, there is always only one virtual function table pointer in the object.
To explore the memory layout of C ++ class objects, first write several classes and functions, first write a base class: Then, we study the structure of the sub-class Memory Object Based on Different inheritance conditions.
1. No virtual function set inheritance
- // Subclass 1, without overload of virtual functions
- Class Child1: public Base
- {
- Public:
- Virtual void f1 () {cout<<"Child1: f1"<< Endl;}
- Virtual void g1 () {cout<<"Child1: g1"<< Endl;}
- Virtual void h1 () {cout<<"Child1: h1"<< Endl;}
- Int child1;
- Protected:
- Private:
- };
2. There is a virtual function inherited
- // Subclass 1, without overload of virtual functions
- Class Child1: public Base
- {
- Public:
- Virtual void f1 () {cout<<"Child1: f1"<< Endl;}
- Virtual void g1 () {cout<<"Child1: g1"<< Endl;}
- Virtual void h1 () {cout<<"Child1: h1"<< Endl;}
- Int child1;
- Protected:
- Private:
- };
The memory structure of the subclass of virtual inheritance is completely different from that of normal inheritance. The virtual inherited subclass has a separate virtual function table. In addition, a virtual function table of the parent class is also saved separately. The two parts use a four-byte 0x00000000 as the boundary.
In the sub-class memory, the first is its own virtual function table, then the data member of the sub-class, then 0x0, and then the virtual function table of the parent class, is a data member of the parent class. If the child class does not have its own virtual function, the Child class does not have a virtual function table, but the child class data and the parent class data still need to be separated by 0x0.
Therefore, in virtual inheritance, the data of the Child C ++ class object and the parent class is completely separated. Stores the virtual function tables and data of sub-classes. The data is divided by 0x in the middle. Finally, the virtual functions and data of the parent class are saved. If the subclass reloads the virtual function of the parent class, replace the corresponding function in the parent class virtual function table in the subclass memory.
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design