Adding multiple inheritance to the C + + object Model
As you can tell from single inheritance, only the virtual function table of the base class is expanded in the derived class. If it is multi-inheritance, then how to expand it?
1) Each base class has its own virtual table.
2) the member functions of a subclass are placed in the table of the first base class.
3) in a memory layout, its parent class layout is ordered in order of declaration.
4) the print () function in the virtual table of each base class is overwrite as a subclass of the Print (). This is done to resolve the different base class type pointers to the same child class instance, and to invoke the actual function.
above 3 classes,derived_mutlip_inherit inherit from Base, base_1 of two classes,derived_mutlip_inherit the structure is as follows:
in order to verify the above C + + object Model, we write the following test code.
void Test_multip_inherit () {Derived_mutlip_inherit DMI (3333); cout << "Object DMI start memory address: \t\t" << &dmi << Endl; cout << "virtual function table _vptr_base address: \ t" << (int*) (&DMI) << Endl; cout << "_vptr_base-1th function address: \ t" << (int*) * (int*) (&DMI) << "\ t is destructor address" << Endl; cout << "_vptr_base-2nd function address: \ t" << ((int*) * (int*) (&DMI) + 1) << "\ T"; typedef void (*fun) (void); Fun Pfun = (fun) * (((int*) * (int*) (&DMI) + 1); Pfun (); cout << Endl; cout << "_vptr_base-3rd function address: \ t" << ((int*) * (int*) (&DMI) + 2) << "\ T"; Pfun = (Fun) * (((int*) * (int*) (&DMI)) + 2); Pfun (); cout << Endl; cout << "_vptr_base-4th function address: \ t" << * ((int*) * (int*) (&DMI) + 3) << "end" \ t "; cout << Endl; cout << "Inferred data member IBase address: \t\t" << ((int*) (&DMI) + 1) << "\ t value obtained by address:" << * ((int*) (&DMI) + 1) << Endl; SetconsoletExtattribute (GetStdHandle (std_output_handle), foreground_intensity | Foreground_green); cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << Endl; Setconsoletextattribute (GetStdHandle (std_output_handle), foreground_intensity | foreground_red); cout << "virtual function table _vptr_base1 address: \ t" << ((int*) (&DMI) +2) << Endl; cout << "_vptr_base1-1th function address: \ t" << (int*) * ((int*) (&DMI) +2) << "\ t is destructor address" << Endl; cout << "_vptr_base1-2nd function address: \ t" << ((int*) * ((int*) (&DMI) +2) + 1) << "\ T"; typedef void (*fun) (void); Pfun = (Fun) * ((int*) * ((int*) (&DMI) +2) + 1); Pfun (); cout << Endl; cout << "_vptr_base1-3rd function address: \ t" << * ((int*) * (int*) ((int*) (&DMI) +2) + 2) << "end" \ t "; cout << Endl; cout << "Guess data member IBase1 address: \ t" << ((int*) (&DMI) +3) << "\ t value obtained by address:" << * ((int*) (&DMI) +3) & lt;< Endl; SetconsoletexTattribute (GetStdHandle (std_output_handle), foreground_intensity | Foreground_green); cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << Endl; Setconsoletextattribute (GetStdHandle (std_output_handle), foreground_intensity | foreground_red); cout << "Guess data member iderived address: \ t" << ((int*) (&DMI) +4) << "\ t value obtained by address:" << * ((int*) (&DMI) +4 ) << Endl;}
The output looks like this:
The object model of the C + + object Model 5--multiple inheritance