C++ diamond problem

2,851 views

Published on

Discussion, concern to the Diamond Problem of Multiple Inheritance in C++.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,851
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C++ diamond problem

  1. 1. [Year]Saket Kr. PathakSoftware Developer2d/3d Graphics<br />[ C++ Diamond Problem ] Discussion, concern to the Diamond Problem of Multiple Inheritance in C++.<br />Two friends taking burger to the common.<br />Hello all my mate... few days back, one of my friend close to my heart because of this Qs. :) asked me ... in a tea stall (the gathering place near my home, at evening after returning back from Office) ... <br />Yup, unfortunately after days ... today I got time to read a bit more and trying to represent my view ... <br />Let's see a code-snippet ... <br />//Base class<br />class Burger<br />{<br />public:<br /> Burger(int); //Simple parameterized constructor<br />  virtual ~Burger();//Normal Override Destruct-or to avoid memory leak<br />  virtual void McVeggie();//General Virtual Function to override in Derived Classes<br />  virtual void cafeMocha();//General Virtual Function to override in Derived Classes<br />}; <br />//Derived class - 1st<br />class Guy1   :   public Burger<br />{<br /> public:<br />    Guy1(int); //Simple parameterized constructor<br />    virtual ~Guy1();//Normal Override Destruct-or to avoid memory leak<br />    virtual void McVeggie();//General Virtual Function to override in Derived Classes<br />    virtual void cafeMocha();             //General Virtual Function to override in Derived Classes<br />};<br /> <br />//Derived class - 2nd <br />class Guy2   :   public Burger<br />{<br /> public:<br />    Guy2(int);//Simple parameterized constructor<br />    virtual ~Guy2();//Normal Override Destruct-or to avoid memory leak<br />    virtual void McVeggie();//General Virtual Function to override in Derived Classes<br />    virtual void cafeMocha();//General Virtual Function to override in Derived Classes<br />};<br /> <br /> <br />//Derived class – 3rd<br />class CommonFriend   :   public Guy1, public Guy2<br />{<br /> public:<br />    CommonFriend();//Simple parameterized constructor<br />    virtual ~CommonFriend();//Normal Override Destruct-or to avoid memory leak<br />};<br /> <br />Now when we call a burger of either type for the common friend like;<br /> <br />int main()<br />{<br />            CommonFriend needBurger;<br />            needBurger.McVeggie();                                 //Error type Ambiguous<br />            needBurger.cafeMocha();                               //Error type Ambiguous<br /> <br />            return 0;<br />}<br /> <br />This will create Compiler Error for "Ambiguous Type". Now why ... ???<br />As we all know through the concept of overriding in inheritance we simple put a new definition for the function having same signature from base class, these are maintained by compiler by providing virtual function table (vtable) for classes Guy1 and Guy2. <br />But here in 3rd level of inheritance that is indeed multiple inheritances, the provided virtual function table has the pointer for both Guy1 and Guy2 classes which contains their respective pointer of Burger. <br />As the result when we are calling for Mc.Veggie and cafeMocha, the compiler gets ambiguous to make it happen. To avoid this we can do it ... we need to do ...<br />//Derived class - 1st <br />class Guy1   :   public virtual Burger<br />{<br />            public:<br />                     //Same as above<br />}; <br />//Derived class - 2nd <br />class Guy2   :   public virtual Burger<br />{<br />            public:                     <br />                     //Same as above<br />};<br /> <br />Now when the virtual function table for CommonFriend will be created then Compiler will mind the above structure and keep single instance of all the above base classes i.e Bueger, Guy1, Guy2. <br />That's all I think we need to do with multiple inheritance and Diamond problem. <br />Yes people may surprise with the naming conventions of class, functions and instances. It's my choice I had tried to represent the problem in layman terms that will be easily imaginable as well as understandable instead of tricky words or random Letters ... :) that's my way ... yup I will expect comments from you C++ buddies ... so welcome ... :)<br /> <br />References:<br />http://en.wikipedia.org/wiki/Multiple_inheritance<br />http://www.cprogramming.com/tutorial/virtual_inheritance.html<br />http://www.cs.cmu.edu/~donna/public/malayeri.TR08-169.pdf<br />

×