The object model of the C + + object Model 5--multiple inheritance

Source: Internet
Author: User

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*) (&AMP;DMI) << Endl;    cout << "_vptr_base-1th function address: \ t" << (int*) * (int*) (&AMP;DMI) << "\ t is destructor address" << Endl;    cout << "_vptr_base-2nd function address: \ t" << ((int*) * (int*) (&AMP;DMI) + 1) << "\ T";    typedef void (*fun) (void);    Fun Pfun = (fun) * (((int*) * (int*) (&AMP;DMI) + 1);    Pfun ();    cout << Endl;    cout << "_vptr_base-3rd function address: \ t" << ((int*) * (int*) (&AMP;DMI) + 2) << "\ T";    Pfun = (Fun) * (((int*) * (int*) (&AMP;DMI)) + 2);    Pfun ();    cout << Endl;    cout << "_vptr_base-4th function address: \ t" << * ((int*) * (int*) (&AMP;DMI) + 3) << "end" \ t ";    cout << Endl; cout << "Inferred data member IBase address: \t\t" << ((int*) (&AMP;DMI) + 1) << "\ t value obtained by address:" << * ((int*) (&AMP;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*) (&AMP;DMI) +2) << Endl;    cout << "_vptr_base1-1th function address: \ t" << (int*) * ((int*) (&AMP;DMI) +2) << "\ t is destructor address" << Endl;    cout << "_vptr_base1-2nd function address: \ t" << ((int*) * ((int*) (&AMP;DMI) +2) + 1) << "\ T";    typedef void (*fun) (void);    Pfun = (Fun) * ((int*) * ((int*) (&AMP;DMI) +2) + 1);    Pfun ();    cout << Endl;    cout << "_vptr_base1-3rd function address: \ t" << * ((int*) * (int*) ((int*) (&AMP;DMI) +2) + 2) << "end" \ t ";      cout << Endl; cout << "Guess data member IBase1 address: \ t" << ((int*) (&AMP;DMI) +3) << "\ t value obtained by address:" << * ((int*) (&AMP;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*) (&AMP;DMI) +4) << "\ t value obtained by address:" << * ((int*) (&AMP;DMI) +4 ) << Endl;}

The output looks like this:

The object model of the C + + object Model 5--multiple inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.