Diamond Inheritance of C + +

Source: Internet
Author: User

What do we talk about when we talk about C + +?

Encapsulation, inheritance, polymorphism. This is the three main features of the C + + language, and each time we talk about inheritance we inevitably have to talk about a very important question-diamond inheritance.

A. What is Diamond inheritance?

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/0C/wKioL1dJRWXj5DLqAAERlUlgRjw139.png "title=" Diamond inheritance. PNG "alt=" Wkiol1djrwxj5dlqaaerlulgrjw139.png "/>

For example, diamond inheritance is where multiple classes inherit the same common base class, and these derived classes are inherited by a class at the same time. What's wrong with this, let's take a look at a piece of code!

#include <iostream>using namespace Std;class base{protected:int _base;public:void fun () {cout << Base::fun "<< Endl;}}; Class A:public base{protected:int _a;}; Class B:public base{protected:int _b;}; Class D:p ublic A, public b{private:int _d;}; int main () {D d;d.fun ();//Compiler Error: Call ambiguous GetChar ();}

We can see that the object model of D holds two copies of base, and when we want to invoke the fun that we inherited from base, we get the problem of calling ambiguity , and it can result in data redundancy, so it's OK to just one copy, and we save two copies.

So how can we solve it?

The first workaround, use the domain to qualify the function we need to access

int main () {D d;d.a::fun ();d. B::fun (); GetChar ();}

This is not a problem, but it is very inconvenient, and when the program is very large, it can cause confusion in our minds.

So, C + + gave us a different solution-- virtual inheritance

B. Virtual inheritance

What is virtual inheritance?

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/0D/wKioL1dJS2vgVa5qAAESPEZ5yi0755.png "title=" Virtual inheritance of diamond inheritance. PNG "alt=" Wkiol1djs2vgva5qaaespez5yi0755.png "/>

For example, virtual inheritance allows A and B to add the Virtural keyword when inheriting base, it is important to remember that not D uses virtual inheritance

So, how does virtual inheritance solve these annoying problems?

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/0D/wKioL1dJTcGAuLwoAAB1mzWLEJs241.png "title=" The virtual inheritance method of diamond inheritance. PNG "alt=" Wkiol1djtcgaulwoaab1mzwlejs241.png "/>

We can see that the contents of the base are no longer saved in A and B, a copy of the offset address is saved, and then the data of base is saved in a common location so that the data redundancy is reduced, and we can use D.fun () directly to invoke the fun function in base.

#include <iostream>using namespace Std;class base{protected:int _base;public:void fun () {cout << Base::fun "<< Endl;}}; Class A:virtual public base{protected:int _a;}; Class B:virtual public base{protected:int _b;}; Class D:p ublic A, public b{private:int _d;}; int main () {D d;d.fun (); GetChar ();}

* virtual inheritance and virtual functions are completely different two concepts , I hope you do not mix, want to know the virtual function of the students can see the blogger's another blog post "C + + inheritance & polymorphism" http://zimomo.blog.51cto.com/ 10799874/1752936


This article is from the "Zimomo" blog, make sure to keep this source http://zimomo.blog.51cto.com/10799874/1784074

Diamond Inheritance of C + +

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.