C++ diamond problem

saketBit 2,137 views 5 slides Oct 17, 2011
Slide 1
Slide 1 of 5
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5

About This Presentation

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


Slide Content

[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); //Simple parameterized
constructor
virtual ~Burger(); //Normal Override Destruct -
or to avoid memory leak
virtual void McVeggie(); //General Virtual Function to
override in Derived Classes
virtual void cafeMocha(); //General Virtual Function to
override in Derived Classes
};



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

public:
Guy1(int); //Simple parameterized
constructor
virtual ~Guy1(); //Normal Override Destruct -
or to avoid memory leak
virtual void McVeggie(); //General Virtual Function
to override in Derived Classes
virtual void cafeMocha(); //General Virtual
Function to override in Derived Classes
};


//Derived class - 2nd
class Guy2 : public Burger
{
public:
Guy2(int); //Simple parameterized
constructor
virtual ~Guy2(); //Normal Override Destruct -
or to avoid memory leak
virtual void McVeggie(); //General Virtual Function
to override in Derived Classes
virtual void cafeMocha(); //General Virtual
Function to override in Derived Classes
};


//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( );
//Error type Ambiguous
needBurger.cafeMocha();
//Error type Ambiguous

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