Summary: 1, according to the 1 inheritance sequence first arranged based on each parent class structure. 2. The structure includes: a virtual table based on the parent class, a table of the virtual base class of the parent class, a member variable of the parent class of the parent class, and a member variable of the parent class. 3, multiple inheritance and continuous inheritance, virtual function table in order to arrange the function and virtual function inheritance. 4. The member variables of the sub-class are then arranged. 5. The virtual function table of the virtual base class is arranged. 6. member variables of virtual base class
#类中的元素
0. Member variable 1. member function 2. static member Variable 3. static member function 4. virtual function 5. Pure virtual function
#影响对象大小的因素
0. Member variable 1. virtual function table pointer (_VFTPTR) 2. Virtual base class table pointer (_VBTPTR) 3. Memory alignment
The initialization of _vftptr and _vbtptr is done automatically by the constructor of the object, the assignment operator, and after the end of the object's life cycle, is destroyed by the object's destructor.
The type that the object is associated with (type_info), usually placed in the first slot of virtual table.
Virtual inheritance: The inheritance relationship of the virtual keyword is included in the inheritance definition;
Virtual base class: The base class that inherits from virtual in the virtual inheritance system, it should be noted that:
Class Cderive:public virtual CBase {}; Where CBase is called the virtual base class of cderive, rather than saying that CBase is a virtual base class, because CBase can also be a base class that is not a virtual inheritance system.
After a virtual function is derived, it is still a virtual function, even if the virtual keyword is omitted from the derived class.
Note: "_vbptr is _vbtptr" below.
#对象内存布局分类讨论
VC6 in the variable viewer (LOCALS,WATCH1, etc.), you can also see the partial object layout (incomplete, and virtual inheritance is wrong).
VS2005 and later versions of the compiler provide the/d1reportsingleclasslayout[class name] Compilation option to view the full memory layout of an object:
CL Classlayout.cpp/d1reportsingleclasslayoutcchildren
0. Single class
(1). Empty class
sizeof (Cnull) =1 (used to identify the object)
(2). Class with member variables only
int nvarsize = sizeof (cvariable) = 12
Memory layout:
(3). Classes with only virtual functions
int nvfuntionsize = sizeof (cvfuction) = 4 (virtual table pointer)
Memory layout:
(4). Classes with member variables, virtual functions
int nparentsize = sizeof (cparent) = 8
Memory layout:
1. Single inheritance (with member variables, virtual functions, virtual function overrides)
int nchildsize = sizeof (Cchildren) = 12
Results shown in VC (note: There are also 1 virtual function cchildren::g1 not shown):
D1reportsingleclass View:
Memory layout:
2. Multiple inheritance (with member variables, virtual functions, virtual function overrides)
int nchildsize = sizeof (cchildren) = 20
Results shown in VC (note: There are also 2 virtual function cchildren::f2,cchildren::h2 not shown, the this pointer's adjustor[adjustment value] is not printed):
D1reportsingleclass View:
Memory layout:
3. Inheritance with a depth of 2 (with member variables, virtual functions, virtual function overrides)
int ngrandsize = sizeof (cgrandchildren) = 24
The results shown in the VC (note: There are 3 virtual functions cgrandchildren::f2,cchildren::h2,cgrandchildren::f3 not shown, the adjustor[adjustment value of this pointer] is not printed):
D1reportsingleclass View:
Memory layout:
4 repeating inheritance (with member variables, virtual functions, virtual function overrides)
int ngrandsize = sizeof (cgrandchildren) = 28
VC Display Results (note: There are a large number of virtual functions are not displayed, the adjustor[adjustment value of this pointer] is not printed):
thunk function: A form real conversion auxiliary function, mainly to do this pointer adjustment, function call redirection.
D1reportsingleclass View:
Memory layout:
Since M_nage has two copies in the content, we cannot access the variable directly through Pgrandchildrena->m_nage.
This would be ambiguous, and the compiler would not know whether to access the m_nage in CChildren1 or M_nage in CChildren2.
In order to identify a unique m_nage, you need to bring the class name with its scope. As follows:
1 Pgrandchildrena->cchildren1::m_nage = 1;2 pgrandchildrena->cchildren2::m_nage = 2;
5. Single virtual inheritance (with member variables, virtual functions, virtual function overrides)
int nchildsize = sizeof (cchildren) = 20
D1reportsingleclass View:
Memory layout:
6. Multiple virtual inheritance (with member variables, virtual functions, virtual function overrides)
(1) Virtual CParent1, CParent2
int nchildsize = sizeof (cchildren) = 24
D1reportsingleclass View:
Memory layout:
(2) CParent1, virtual CParent2
int nchildsize = sizeof (cchildren) = 24
D1reportsingleclass View:
Memory layout:
(3) Virtual CParent1, virtual CParent2
int nchildsize = sizeof (cchildren) = 28
D1reportsingleclass View:
Memory layout:
7. Virtual multiple inheritance of diamond type (with member variables, virtual functions, virtual function overrides)
int ngrandchildsize = sizeof (Cgrandchildren) = 36
D1reportsingleclass View:
thunk function: A form real conversion auxiliary function, mainly to do this pointer adjustment, function call redirection.
Memory layout:
#外部参考
Memory structure for C + + classes
Chenhao-C + + virtual function table parsing
Chenhao-memory layout for C + + objects (top)
Chenhao-memory layout for C + + objects (bottom)
C + + Object memory Model "Memory Layout" (RPM)