The multiple inheritance of a class can be broadly divided into two situations. One has no common base class. One has a common base class.
Let's look at the first case:
1 #include <stdio.h> 2 class Xuzhina_dump_c06_s5_mother 3 {4 private:5 int m_age; 6 int m_beauty; 7 Public:8 virtual void print () 9 {ten printf ("mother\n"); 11} 12 13 virtual void setbeauty (int age, int beauty) {m_age = age-5; m_beauty = Bea Uty; +};19 class Xuzhina_dump_c06_s5_father {private:23 int m_strong; int m_a Ge public:26 virtual void print () ("father\n"); irtual void Setstrong (int strong, int age)-{M_strong = strong; m_age = age; 34 } 35}; 3637 class Xuzhina_dump_c06_s5_child:public Xuzhina_dump_c06_s5_father, $ public Xuzhina_dump_c06_s5_mother 39 40 { private:42 bool M_newmind; public:44 virtual void print () 45 {46 printf ("child\n"); Setgender (bool gender) {M_newmind = true; 52 if (gender)-------setbeauty (ten); {Setstrong (20,20); 59} 60} 61}; 6263 int main (), {xuzhina_dump_c06_s5_child* child = new Xuzhina_dump_c06_s5_child; Child->setgender ( FALSE); Child->print (); xuzhina_dump_c06_s5_father* f = child; F->print (); xuzhina_dump_c06_s5_mother* m = child; M->print (); 0; 76}
Take a look at the assembly of the main function:
(GDB) disassemble maindump of assembler code for function main:0x080485b0 <+0>: Push%EBP 0X080485B1 < +1>: mov%esp,%ebp 0x080485b3 <+3>: Push%ebx 0x080485b4 <+4>: and $0xfffffff0,%esp 0x080485b7 <+7>: Sub $0x20,%esp 0x080485ba <+10>: Movl $0x1c, (%ESP) 0x080485c1 <+17>: Call 0x8048490 <[email protected]> 0x080485c6 <+22>: mov%eax,%ebx 0x080485c8 <+24> : mov%ebx, (%ESP) 0X080485CB <+27>: Call 0x8048746 <_ZN25xuzhina_dump_c06_s5_childC2Ev> 0x08048 5d0 <+32>: mov%ebx,0x1c (%ESP) 0x080485d4 <+36>: mov 0x1c (%ESP),%eax 0x080485d8 <+40>: MOV (%EAX),%eax 0x080485da <+42>: add $0x8,%eax 0x080485dd <+45>: mov (%EAX),%eax 0x080 485DF <+47>: Movl $0x0,0x4 (%ESP) 0x080485e7 <+55>: mov 0x1c (%ESP),%edx 0x080485eb <+59>: mov%edx, (%ESP)0x080485ee <+62>: Call *%eax 0x080485f0 <+64>: mov 0x1c (%ESP),%eax 0x080485f4 <+68>: M OV (%eax),%eax 0x080485f6 <+70>: mov (%EAX),%eax 0x080485f8 <+72>: mov 0x1c (%ESP),%edx 0x 080485FC <+76>: mov%edx, (%ESP) 0x080485ff <+79>: Call *%eax 0x08048601 <+81>: mov 0x1c (%ESP),%eax 0x08048605 <+85>: mov%eax,0x18 (%ESP) 0x08048609 <+89>: mov 0x18 (%ESP),%eax 0x0804860d <+93>: mov (%EAX),%eax 0x0804860f <+95>: mov (%EAX),%eax 0x08048611 <+97>: mov 0x18 (%ESP),%edx 0x08048615 <+101>: mov%edx, (%ESP) 0x08048618 <+104>: Call *%eax 0x0804 861A <+106>: Cmpl $0x0,0x1c (%ESP) 0x0804861f <+111>: je 0x804862a <main+122> 0x08048621 &l T;+113>: mov 0x1c (%ESP),%eax 0x08048625 <+117>: add $0xc,%eax 0x08048628 <+120>: jmp 0x8 04862f <main+127> 0X0804862A <+122>: mov $0x0,%eax 0x0804862f <+127>: mov%eax,0x14 (%esp) 0x08048633 <+131> ;: mov 0x14 (%ESP),%eax 0x08048637 <+135>: mov (%EAX),%eax 0x08048639 <+137>: mov (%EAX),%ea x 0x0804863b <+139>: mov 0x14 (%ESP),%edx 0x0804863f <+143>: mov%edx, (%ESP) 0x08048642 <+14 6>: Call *%eax 0x08048644 <+148>: mov $0x0,%eax 0x08048649 <+153>: mov-0x4 (%EBP),%EBX 0X0804864C <+156>: Leave 0x0804864d <+157>: ret End of assembler dump.
By the above assembly, you can see that the object of the child's address is stored in esp+0x1c
And here are a few instructions
0x08048601 <+81>: mov 0x1c (%esp),%eax 0x08048605 <+85>: mov %eax,0x18 (%ESP) 0x08048609 <+89>: mov 0x18 (%esp),%eax 0x0804860d <+93>: mov (%eax),%eax 0x0804860f <+95>: mov (%eax),%eax 0x08048611 <+97>: mov 0x18 (%ESP),%edx 0x08048615 <+101>: mov %edx, (%ESP) 0x08048618 <+104>: call *%eax 0x08048621 <+113>: mov 0x1c (%esp),%eax 0x08048625 <+117>: add $0xc,% EAX 0x0804862f <+127>: mov %eax,0x14 (%esp) 0x08048633 <+131>: mov 0x14 (%ESP),%eax 0x08048637 <+135>: mov (%eax),%eax 0x08048639 <+137>: mov (%eax),%eax 0x0804863b <+139>: mov 0x14 (%esp),%edx 0x0804863f <+143>: mov %edx, (%ESP) 0x08048642 <+146>: call *%eax
Because it is and the code
xuzhina_dump_c06_s5_father* f = child; f->print (); xuzhina_dump_c06_s5_mother* m = Child M->print ();
corresponding to the.
From
0x08048625 <+117>: add $0xc,%eax
It is very strange to see that when a pointer to a class xuzhina_dump_c06_s5_child is converted to a pointer of class Xuzhina_dump_c06_s5-_mother, it is not directly assigned to the past, but instead adds an offset value to the expected address.
So how does this offset 0xC come from? Based on the previous section's experience, take a look at the assembly of Constructors:
(GDB) disassemble _zn25xuzhina_dump_c06_s5_childc2evdump of assembler code for function _zn25xuzhina_dump_c06_s5_ childc2ev:0x08048746 <+0>: Push%ebp 0x08048747 <+1>: mov%esp,%ebp 0x08048749 <+3>: Sub $0x18,%esp 0x0804874c <+6>: mov 0x8 (%EBP),%eax 0x0804874f <+9>: mov%eax, (%ESP) 0x08048752 <+12>: Call 0x804872a <_ZN26xuzhina_dump_c06_s5_fatherC2Ev> 0x08048757 <+17>: mov 0x8 (%EBP),%eax 0x0804875a <+20>: add $0xc,%eax 0x0804875d <+23>: mov%eax, (%ESP) 0x08048 760 <+26>: Call 0x8048738 <_ZN26xuzhina_dump_c06_s5_motherC2Ev> 0x08048765 <+31>: mov 0x8 (% EBP),%eax 0x08048768 <+34>: Movl $0x8048848, (%eax) 0x0804876e <+40>: mov 0x8 (%EBP),%eax 0x080 48771 <+43>: Movl $0x804885c,0xc (%eax) 0x08048778 <+50>: Leave 0x08048779 <+51>: ret End of assembler dump.
From above, the class Xuzhina_dump_c06_s5_child object is actually a combination of two class Xuzhina_dump_c06_s5_father,xuzhina_dump_c06_s5_mother objects, The offset value of 0xC is exactly the size of the class Xuzhina_dump_c06_s5_father (virtual function table pointer + two member variable m_strong,m_age).
And by 0x08048768, 0x08048771 two instructions, class Xuzhina_dump_c06_s5_child store two virtual function table, respectively, stored in 0x8048848, 0x804885c.
Take a look at what these two tables hold separately.
The first one:
(gdb) x/4x 0x80488480x8048848 <_ztv25xuzhina_dump_c06_s5_child+8>: 0x080486a8 0x08048690 0X080486C4 0xfffffff4 (gdb) info symbol 0x080486a8xuzhina_dump_c06_s5_child::p rint () in section. Text of/home/ BUCKXU/WORK/6/5/XUZHINA_DUMP_C6_S5 (GDB) info symbol 0x08048690xuzhina_dump_c06_s5_father::setstrong (int, int) in Section. Text OF/HOME/BUCKXU/WORK/6/5/XUZHINA_DUMP_C6_S5 (GDB) info symbol 0x080486c4xuzhina_dump_c06_s5_child:: Setgender (bool) in section. Text of/home/buckxu/work/6/5/xuzhina_dump_c6_s5
The second one:
(gdb) x/4x 0x804885c0x804885c <_ztv25xuzhina_dump_c06_s5_child+28>: 0X080486BC 0x08048662 0x00000000 0x00000000 (GDB) info symbol 0x080486bcnon-virtual thunk to Xuzhina_dump_c06_s5_child::p rint () in section. Text of/h OME/BUCKXU/WORK/6/5/XUZHINA_DUMP_C6_S5 (GDB) info symbol 0x08048662xuzhina_dump_c06_s5_mother::setbeauty (int, int) In sections. Text OF/HOME/BUCKXU/WORK/6/5/XUZHINA_DUMP_C6_S5 (GDB) disassemble 0x080486bcdump of assembler code for FUNCTI On _zthn12_n25xuzhina_dump_c06_s5_child5printev: 0X080486BC <+0>: subl $0xc,0x4 (%ESP) 0X080486C1 <+5>: jmp
From the above, the object memory distribution of class Xuzhina_dump_c06_s5_child is as follows:
From the above, in the case of multiple inheritance, there is such a conclusion:
1. There are multiple virtual function tables.
2. The size of the class object is equal to the size of each base class (the virtual function table pointer + member variable) and the sum of its own member variables.
3. The "Hidden objects" order of each base class in the subclass is arranged in the order of inheritance, independent of the Declaration/definition order of the base class.
4. When a subclass object pointer is converted to a base class pointer, it is actually assigned to the base class pointer to the corresponding base class "implied object" address of the object class.
For multiple inheritance with common base classes, you can also explore them in the same way. For base classes and subclasses with the same name virtual function, you can also explore.
"Research on the principle of Coredump" Linux version x86 6.7-section multiple inheritance