For simple objects, the memory structure layout is very simple and follows the following description:
1. Non-static data members are the main factors affecting the memory size occupied by objects, which increases with the increase of objects;
2. All objects share a static data member. When a sizeof is used to calculate an object, it will not contain static data members to occupy the memory size;
3. Static and non-static member functions do not affect the object memory size;
4. If the object contains a virtual function, four bytes of space will be added to save the virtual function table pointer.
The following describes a single inheritance class:
There is a base class instance in the instance header of the derived class, and the virtual function table shares the same base class. The memory layout of the base class is the same as that of the simple object. The memory layout of the derived class is the memory layout composed of the memory layout of the base class and the members added by the derived class, at the same time, you only need to modify the virtual function table of the base class without adding the virtual function table pointer. For a class, the entireProgramThere is only one virtual function table, regardless of the number of objects.
Multi-inheritance is basically the same as single inheritance, with only one exception, when a derived class is derived from two parent classes with a common base class, two instance objects of the parent class of the derived class will be created. The solution is to use virtual inheritance, virtual inheritance can generate only one parent class instance.