Re: Why multiple inheritance fails
On Tue, 26 Jun 2007 08:41:20 -0700, "Vijay Visana"
<vijay_visana@myorganization.com> wrote:
thanks
Yes I should not have not used daimond to represent it ( like many C++ Books
do)
That makes me think you don't understand the problem, because "many C++
books" don't misrepresent non-virtual inheritance.
Yes using virtual base class will solve the problem
Now I think you may indeed understand the problem. :)
but I want to know why
it works in first case and does not work when Interfaces (pure ABC) has loop
in inheritance hierarchy.
Inheritance hierarchies are directed acyclic graphs, so I'd avoid saying
they have "loops", because traversing a "loop" takes you back to where you
started (cyclic), even if it's a one-way (directed) loop.
I have read this in Don Box master pieace "Essential COM" where Interfaces
are not allowed to have multiple inheritance but implementation can have MI.
This is just out of plane curiosity.
It worked in the first case because the static type of pCatDog in:
pCatDog->walk();//Don't get error
is CatDog*, CatDog::walk exists, name lookup finds it immediately, and base
classes aren't even considered. It failed in the second case:
pPerCat->walk(); //Get Error here why I don't know
because the static type of pPerCat is persianCat*, and the inheritance
graph of persianCat looks like this:
A A
| |
B----C
|
persianCat
To find "walk", name lookup has to go all the way up to the A base classes,
where it finds two equally good "walk" functions it could call in the
separate instances of A, so you get an ambiguity error. Having B and C
inherit A virtually ensures there's only instance of A, which means there's
only one A::walk, so no ambiguity.
--
Doug Harrison
Visual C++ MVP