Inner implementation of diamond inheritance

Source: Internet
Author: User
Tags inheritance

Problem:

Because the following diagram is defined as a multiple-inheritance type, the subclass will have two semantics and data redundancy, and the diamond inheritance will resolve these problems, and what happens to the diamond inheritance. How it was achieved.


This time try to explain the mechanism of diamond inheritance (Implementation method)


Build multiple inheritance in the diagram above and write the code:

class base {public:  virtual void func1 ()  {  cout <<  "Base::
Func1 () " << endl;
 } protected:  int _a;

}; class base1: public base {public:  virtual void func1 ()  {  cout
 <<  "Base1::func1 ()"  << endl;  }  virtual void func3 ()  {  cout <<  "base1::func3 ()"  
<< endl;
 } protected:  int _b;

}; class base2 : public base {public:  virtual void func2 ()  {  
cout <<  "Base2::func2 ()"  << endl;  }  virtual void func4 ()  {  cout <<  "Base2::func4 ()"  
<< endl;
 } protected:  int _c;

}; Class derive : public base1, public base2 {public: &NBSP;VIRTUAL&NBSP;VOID&NBSP;FUNC1 ()    {  cout <<  "derive::func1 ()"  << endl;  } &NBSP;VIRTUAL&NBSP;VOID&NBSP;FUNC2 ()  {  cout <<  "Derive::func2 ()"  <<
 endl;  }  virtual void func3 ()  {  cout <<  "derive::func3 ()"  
<< endl;  }  virtual void func4 ()  {  cout <<  "Derive::func4 ()"  
<< endl;  }  virtual void func5 ()  {  cout <<  "Derive::func5 ()"  
<< endl;
 } protected:  int _d;


};

Typedef void (*func) (); Void pfun (int *vtable) { for  (int i = 0; vtable[i] != 0;  ++i)  {  printf ("First  %d  virtual function-> %p\n",  i, vtable[i]);   
func f =  (FUNC) vtable[i];
  f ();  }}
void Test6 ()
{
Base A;
Base1 b;
Base2 C;
Derive D;

int sb = sizeof (a);
int sb1 = sizeof (b);
int SB2 = sizeof (c);
int sd = sizeof (d);

Pfun ((int*) * (int*) &d);
printf ("\ n");
Pfun ((int*) (* (int*) &d +));
printf ("\ n");
}


Multiple inheritance Run Result: (virtual table pointer address can be obtained by _vfptr of runtime "D")


Can be seen: Base1, Base2 have func1 (), Base1 _vfptr and Base2 different _vfptr address, pointing to the content is also different Base1 virtual table and BASE2 virtual table all contain the Func () of base, this inheritance has ambiguity and redundancy.


When defining Base1,base2, add virtual to the public base to change this inheritance to Diamond inheritance.

Diamond Inheritance Run Result:


Diamond inheritance solves the problem of two semantics and redundancy.

Multiple Inheritance calculation size:


Diamond inheritance is calculated in size:


Combine multiple inheritance with diamond inheritance to analyze:


As can be seen from the above figure, the superclass is the same size when the diamond inherits from multiple inheritance, but differs from the parent class starting size, the parent class has 8 more bytes, and the subclass has 18 bytes. What has been done about it.
In order to eliminate ambiguity and redundancy, you have to change the base part of Base1 and Base2 into a copy, which only changes the base part of Base1, Base2 to the base part. That's how it's going to come true.


Open the interior of "D"


Found a base, and then the Base1, Base2, base are open.


See: Base1 's _vfptr,base2 _vfptr,base's _vfptr address is the same, pointing to the same content is the virtual table of base.

Through the above image and call Memory window, the corresponding address is analyzed to get the following image

From the above analysis can be obtained: compared to multi-inheritance, diamond inheritance in the subclass will be more than 8 bytes (two pointers), because in the subclass of the parent class inherits the part of each additional pointer, the action is to point to an address, the address of the parent class to hold the increment of the address of the pointer and the address offset value of the superclass, The superclass member part is found, and the two parent pointers all point to the same space.

In this way, the parent class and the public part of the superclass are the same storage space, which solves the problem of two semantics and data redundancy.



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.