Re: Is there any more benifits by virtual inheritance than resolving
the "diamond problem" ?
James Kanze wrote:
On Nov 25, 9:34 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
James Kanze wrote:
What's the "diamond problem"?
Wikipedia is your friend.
http://en.wikipedia.org/wiki/Diamond_problem
OK. The article isn't written all that clearly, but if I
understand the first section correctly, the problem would only
occur in C++ if you used virtual inheritance; virtual
inheritance creates the diamond problem, rather than solves it.
No, virtual inheritance is a solution to the diamond problem.
The diamond problem happens with multiple inheritance when the
multiple base classes have a common base class of their own. The problem
is: Should this common base class be duplicated in the
multiple-inherited class, or should it be merged into one?
If, for example, class A has a member function named foo(), class B
inherits from A, class C inherits from A, and finally class D inherits
from both B and C, and then you create an object of type D and call its
foo() member function, what should happen?
Moreover, if a function in B calls a function in A which modifies some
member variable of A, and some other function in C calls that same
function in A, what should happen? Should both the B and C calls cause
the *same* member variable to be modified, or should these two members
be separate (so that B has its own state, separate from C which has its
own)?
Suppose you have an object of type D. If some function somewhere
expects an object of type A, and a member function of B gives 'this' to
it, what should happen? Likewise if a member function of C calls that
function taking an object of type A. Should they pass their common A
sub-object, or should they pass separate A sub-objects? (This can make a
huge difference not only on the state of the A part of the object, but
because A might have some virtual function which both B and C implement.)
Not using virtual inheritance is taking one stance (B and C should be
separate and have no common base), while using virtual inheritance is
taking another (their bases should be merged). Thus virtual inheritance
offers one solution to the problem.
Of course, the next section, "Approaches", seems to be
dicussing something else; it certainly doesn't discuss how C++
handles the problem described immediately above.
It discusses how some languages supporting multiple inheritance deal
with the problems.
In other words, the author of the article doesn't seem to know
what the "diamond problem" is himself, since he discusses two
different things under the vocable.
I think it's you who is not understanding what the problem is (which,
quite frankly, I find quite surprising).