Inner implementation of diamond inheritance

Source: Internet
Author: User

Problem:

Because the subclass will have two semantics and data redundancy when it is defined as a multi-inheritance type, and the diamond inheritance solves these problems, what happens to the diamond inheritance? And how did it come true?


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


650) this.width=650; "title=" Untitled. png "alt=" Wkiom1bgktngoqhsaaajusl9uyw429.png "src=" http://s3.51cto.com/wyfs02/M02 /7d/1f/wkiom1bgktngoqhsaaajusl9uyw429.png "/>

Follow the build multiple inheritance and write the code:

CLASS&NBSP;BASE{PUBLIC:&NBSP;VIRTUAL&NBSP;VOID&NBSP;FUNC1 ()  {  cout <<  "Base:: Func1 () " << endl; }protected: int _a;}; CLASS&NBSP;BASE1:&NBSP;PUBLIC&NBSP;BASE{PUBLIC:&NBSP;VIRTUAL&NBSP;VOID&NBSP;FUNC1 ()  {  cout  <<  "Base1::func1 ()"  << endl; } virtual void func3 ()   {  cout <<  "base1::func3 ()"  << endl; }protected: int  _b;}; CLASS&NBSP;BASE2&NBSP;:&NBSP;PUBLIC&NBSP;BASE{PUBLIC:&NBSP;VIRTUAL&NBSP;VOID&NBSP;FUNC2 ()  {   cout <<  "Base2::func2 ()"  << endl; } virtual void func4 ()  {  cout <<  "Base2::func4 ()"  << endl; }protected: int  _c;}; class derive : public base1, public base2{public: virtual void  Func1 ()   {  cout <<  "Derive::func1 ()"  << endl; } virtual void 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 ("section  %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 + 20)); printf ("\ n"); }


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

650) this.width=650; "title=" qq20160309205552.jpg "alt=" wkiom1bgm1riuagxaadplgo_hoy917.jpg "src="/HTTP/ S2.51cto.com/wyfs02/m01/7d/1f/wkiom1bgm1riuagxaadplgo_hoy917.jpg "/>


650) this.width=650; "title=" qq20160309205154.jpg "alt=" wkiom1bgn2prmdxhaahndd-it2w063.jpg "src="/HTTP/ S2.51cto.com/wyfs02/m01/7d/20/wkiom1bgn2prmdxhaahndd-it2w063.jpg "/>

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:

650) this.width=650; "title=" qq20160309222814.jpg "alt=" wkiom1bgmymb1emvaadb-c6zoy4066.jpg "src="/HTTP/ S1.51cto.com/wyfs02/m00/7d/1f/wkiom1bgmymb1emvaadb-c6zoy4066.jpg "/>


Diamond inheritance solves the problem of two semantics and redundancy.

Multiple Inheritance calculation size:

650) this.width=650; "title=" qq20160309205102.jpg "alt=" wkiol1bgi6ziv-70aacmjqhqlue847.jpg "src="/HTTP/ S4.51cto.com/wyfs02/m02/7d/1d/wkiol1bgi6ziv-70aacmjqhqlue847.jpg "/>


Diamond inheritance is calculated in size:

650) this.width=650; "title=" qq20160309205650.jpg "alt=" wkiom1bgjprqybwnaacqoxsngee350.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m02/7d/1f/wkiom1bgjprqybwnaacqoxsngee350.jpg "/>


Combine multiple inheritance with diamond inheritance to analyze:

650) this.width=650; "title=" size. png "alt=" Wkiom1bgjubraf5raaaanl3sv2u975.png "src=" http://s4.51cto.com/wyfs02/M00/ 7d/1f/wkiom1bgjubraf5raaaanl3sv2u975.png "/>


As can be seen, in the diamond inheritance and multiple inheritance, the superclass size, but from the beginning of the parent class size difference, the parent Class 8 more bytes, the subclass more than 18 bytes. What did it do?
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. And how did that come true?


Open the interior of "D"

650) this.width=650; "title=" qq20160309205733.jpg "style=" Float:none "alt=" wkiol1bgn0fy52wnaadlciml51q829.jpg "src = "Http://s3.51cto.com/wyfs02/M01/7D/1E/wKioL1bgN0fy52wnAADLCiml51Q829.jpg"/>


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

650) this.width=650; "title=" qq20160309230100.jpg "alt=" wkiom1bgosgcvgioaaidmhmu-0a256.jpg "src="/HTTP/ S2.51cto.com/wyfs02/m00/7d/20/wkiom1bgosgcvgioaaidmhmu-0a256.jpg "/>


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

650) this.width=650; "title=" points to function. jpg "style=" float:none; "src=" http://s1.51cto.com/wyfs02/M02/7D/24/ Wkiol1bhbphbriweaaql_-yzy1q066.jpg "alt=" Wkiol1bhbphbriweaaql_-yzy1q066.jpg "/>

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

650) this.width=650; "title=" vrgva.jpg "src=" http://s4.51cto.com/wyfs02/M01/7D/25/ Wkiom1bhbx-yf6ftaashkgzn6lc046.jpg "alt=" Wkiom1bhbx-yf6ftaashkgzn6lc046.jpg "/>

Can be obtained from the analysis: compared to multiple inheritance, diamond inheritance in the subclass will be more than 8 bytes (two pointers), because the subclass of the parent class inherits the part of each additional pointer, the action is to point to an address, the address and the address of the superclass is stored in the offset value, the address and the offset value to find the superclass members, And the two parent pointers all point to the same piece of 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.



Inner implementation of diamond 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.