qwertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfgh
jklzxcvbnmqwertyuiopasdfghjklzxcvb
nmqwertyuiopasdfghjklzxcvbnmqwer
tyuiopasdfghjklzxcvbnmqwertyuiopas
dfghjklzxcvbnmqwertyuiopasdfghjklzx
cvbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyuio
pasdfghjklzxcvbnmqwertyuiopasdfghj
klzxcvbnmqwertyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmqwerty
uiopasdfghjklzxcvbnmqwertyuiopasdf
ghjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmrty
uiopasdfghjklzxcvbnmqwertyuiopasdf
ghjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmqw
C++ Article
Diamond Problem
10/12/2011
Saket Kr. Pathak
Saket kr. Pathak
C++ Article
Saket Kr. Pathak Page 2
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) ...
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 provide 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
Saket kr. Pathak
C++ Article
Saket Kr. Pathak Page 3
virtual ~Guy1(); //Normal Override Destruct-or to provide 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 provide 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 provide 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.
Saket kr. Pathak
C++ Article
Saket Kr. Pathak Page 4
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
Saket kr. Pathak
Top Related