You are on page 1of 5

[Year ]

Saket Kr. Pathak Software Developer 2d/3d Graphics

[ C++

Diamond Problem ]

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

Two friends taking burger to the common.

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) ... Yup, unfortunately after days ... today I got time to read a bit more and trying to represent my view ... Let's see a code-snippet ... //Base class class Burger { public: Burger(int); virtual ~Burger(); leak virtual void McVeggie(); Derived Classes virtual void cafeMocha(); Derived Classes };

//Simple parameterized constructor //Normal Override Destruct-or to avoid memory //General Virtual Function to override in //General Virtual Function to override in

//Derived class - 1st class Guy1 : public Burger

{ public: Guy1(int); virtual ~Guy1(); memory leak virtual void McVeggie(); Derived Classes virtual void cafeMocha(); Derived Classes }; //Derived class - 2nd class Guy2 : public Burger { public: Guy2(int); virtual ~Guy2(); memory leak virtual void McVeggie(); Derived Classes virtual void cafeMocha(); Derived Classes };

//Simple parameterized constructor //Normal Override Destruct-or to avoid //General Virtual Function to override in //General Virtual Function to override in

//Simple parameterized constructor //Normal Override Destruct-or to avoid //General Virtual Function to override in //General Virtual Function to override in

//Derived class 3rd class CommonFriend : public Guy1, public Guy2 { public: CommonFriend(); //Simple parameterized constructor virtual ~CommonFriend(); //Normal Override Destruct-or to avoid memory leak }; Now when we call a burger of either type for the common friend like; int main() { CommonFriend needBurger; needBurger.McVeggie(); needBurger.cafeMocha(); return 0; }

//Error type Ambiguous //Error type Ambiguous

This will create Compiler Error for "Ambiguous Type". Now why ... ??? 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. 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. 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 ... //Derived class - 1st class Guy1 : public virtual Burger { public: //Same as above }; //Derived class - 2nd class Guy2 : public virtual Burger { public: //Same as above }; 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. That's all I think we need to do with multiple inheritance and Diamond problem. 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 ... :)

References:

http://en.wikipedia.org/wiki/Multiple_inheritance http://www.cprogramming.com/tutorial/virtual_inheritance.html http://www.cs.cmu.edu/~donna/public/malayeri.TR08-169.pdf