C + + Diamond inheritance

Source: Internet
Author: User

in C + +, what is called a diamond problem (which can also be called a diamond inheritance problem), how to avoid it?

The chart below can be used to explain the diamond problem.

Suppose we have Class B and Class C, both of which inherit the same Class A. In addition we have Class D, Class D inherits Class B and Class C through multiple inheritance mechanisms. Because the shape of the chart is similar to a diamond (or diamond), this problem is visually called the Diamond problem (Diamond inheritance problem). Now, let's translate the above diagram into a specific code:

[CPP]View Plaincopy
  1. /*
  2. The animal class corresponds to the Class A of the chart
  3. */
  4. Class Animal {/ * ... */}; //base class
  5. {
  6. int weight;
  7. Public
  8. int Getweight () { return weight;};
  9. };
  10. Class Tiger: Public Animal {/ * ... */};
  11. Class Lion: Public Animal {/ * ... */}
  12. Class Liger: Public Tiger, public Lion {/ * ... */};


In the above code, we give a specific example of a diamond problem. The animal class corresponds to the topmost class (A in the chart), and Tiger and Lion correspond to the B and C,liger classes of the chart (Lion Tiger Beast, Tiger and lion hybrids) correspond to D.

Now, the question is what happens if we have this inheritance structure.

Take a look at the following code and then answer the question.

[CPP]View Plaincopy
    1. int main ()
    2. {
    3. Liger LG;
    4. /* Compile error, the following code will not be passed by any C + + compiler */
    5. int weight = Lg.getweight ();

In our inheritance structure, we can see that both the tiger and the lion classes inherit from the animal base class. So the question is: Because Liger inherits the tiger and Lion classes, the Liger class will have two members (data and methods) of the animal class, and the Liger object "LG" will contain two sub-objects of the animal base class.

So, you're going to ask what's wrong with the Liger object that has two animal base class child objects? Take a look at the code above-calling "Lg.getweight ()" will result in a compilation error. This is because the compiler does not know whether to call Getweight () of the Tiger class or getweight () that calls the Lion class. Therefore, calling the Getweight method is ambiguous and therefore cannot be compiled.

Solutions for Diamond Problems:

We gave an explanation of the diamond problem, but now we have to give a solution to the problem of diamonds. If the Lion class and the Tiger class are annotated with virtual when inheriting the animal class, for each Liger object, C + + guarantees that only a child object of the animal class will be created. Take a look at the following code:

[CPP]View Plaincopy
    1. Class Tiger: virtual public Animal {/ * ... */};
    2. Class Lion: virtual public Animal {/ * ... */}


You can see that the only change is that we added the "virtual" keyword to the declarations of class tiger and Class Lion. Now the class Liger object will have only one animal child object, and the following code compiles normally:

[CPP]View Plaincopy
    1. int main ()
    2. {
    3. Liger LG;
    4. /* Since we have declared the "virtual" keyword in the tiger and Lion class definitions, the following code compiles OK */
    5. int weight = Lg.getweight ();
    6. }

Because Java does not support multiple inheritance, there is no diamond inheritance problem. But Java can indirectly implement multiple inheritance through interfaces.

[Java]View Plaincopy
      1. Class Mule implements Horse,donkey
      2. {
      3. /* Horse and Donkey are interface */
      4. }

C + + 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.