You are on page 1of 4

qwertyuiopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmqwertyuiopasdfgh jklzxcvbnmqwertyuiopasdfghjklzxcvb nmqwertyuiopasdfghjklzxcvbnmqwer C++ Article tyuiopasdfghjklzxcvbnmqwertyuiopas Diamond Problem dfghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq wertyuiopasdfghjklzxcvbnmqwertyuio pasdfghjklzxcvbnmqwertyuiopasdfghj klzxcvbnmqwertyuiopasdfghjklzxcvbn mqwertyuiopasdfghjklzxcvbnmqwerty

uiopasdfghjklzxcvbnmqwertyuiopasdf ghjklzxcvbnmqwertyuiopasdfghjklzxc vbnmqwertyuiopasdfghjklzxcvbnmrty uiopasdfghjklzxcvbnmqwertyuiopasdf ghjklzxcvbnmqwertyuiopasdfghjklzxc


10/12/2011 Saket Kr. Pathak
Saket kr. Pathak

C++ Article
What is Diamond problem ... ?

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) ...
Saket kr. Pathak

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(); virtual void McVeggie(); virtual void cafeMocha(); };

//Simple parameterized constructor //Normal Override Destruct-or to provide memory leak //General Virtual Function to override in Derived Classes //General Virtual Function to override in Derived Classes

//Derived class - 1st class Guy1 : public Burger { public: Guy1(int);


Saket Kr. Pathak

//Simple parameterized constructor


Page 2

C++ Article
virtual ~Guy1(); virtual void McVeggie(); virtual void cafeMocha(); }; //Normal Override Destruct-or to provide memory leak //General Virtual Function to override in Derived Classes //General Virtual Function to override in Derived Classes

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

//Simple parameterized constructor //Normal Override Destruct-or to provide memory leak //General Virtual Function to override in Derived Classes //General Virtual Function to override in Derived Classes

//Derived class 3rd class CommonFriend : public Guy1, public Guy2 { public: CommonFriend(); //Simple parameterized constructor Saket kr. Pathak virtual ~CommonFriend(); //Normal Override Destruct-or to provide 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; } 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.

//Error type Ambiguous //Error type Ambiguous

Saket Kr. Pathak

Page 3

C++ Article
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 singlePathak Saket kr. 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

Saket Kr. Pathak

Page 4

You might also like