Re: Diamond Shape Inheritance
On Nov 25, 8:43 am, amit <amitkee...@gmail.com> wrote:
am new to the group. So not sure of the topic has been
discussed.....
I was reading about diamond shaped inheritance on wikipedia.
In which article. The quality of Wikipedia articles is very
variable, and many of the ones on software engineering are not
particularly good.
=======================
In object-oriented programming languages with multiple
inheritance and knowledge organization, the diamond problem is
an ambiguity that arises when two classes B and C inherit from
A, and class D inherits from both B and C. If a method in D
calls a method defined in A (and does not override the
method), and B and C have overridden that method differently,
then from which class does it inherit: B, or C?
======================
Now in the explanation for C++, the explanation goes as below:
=========================
====
C++ by default follows each inheritance path separately, so a
D object would actually contain two separate A objects, and
uses of A's members have to be properly qualified. If the
inheritance from A to B and the inheritance from A to C are
both marked "virtual" (for example, "class B : virtual public
A"), C++ takes special care to only create one A object, and
uses of A's members work correctly. If virtual inheritance and
nonvirtual inheritance are mixed, there is a single virtual A
and a nonvirtual A for each nonvirtual inheritance path to A.
=========================
====
I has the following questions:
Question 1:
But it still does not answer the question raised... Lets
assume Base class (Class A) has a function called show(). Lets
assume Class B and C inherit A (virtually...) and override the
function show(). Now Suppose class D inherits both B and C
(making a diamond shaped inheritance) and we call show().
Which show will be called ? B's Show or C'Show ?
Well, it sort of answers it indirectly, in the first part you
quote. It's ambiguous. In C++, anything that is ambiguous is
an error; the compiler will complain.
Question2:
In the explanation it mentions the following: " a D object
would actually contain two separate A objects". Now my
understanding of C++ is limited. I actually thought when
inheritance happens we have Just 1 object of derived class.
Now as per the above statement we have 3 objects: 1 derived
class object and 2 base class objects (possibly embedded in
the derived class object) !!
An object can have sub-objects. In this case, the object of
type D has two sub-objects, one of type B and one of type C, and
each of these sub-objects has a distinct sub-object of type A.
This kind of confused me. Is this true ?
Well, it would have been better if they'd have said sub-object,
rather than object. But on the whole, yes---there are a few
particular contexts where C++ treats base sub-objects different
than other sub-objects (you can have a base sub-object of an
abstract class, a base sub-object can have 0 size, etc.), but
they are still very much objects in most senses.
The point of course, being that without virtual inheritance, in
C++, the inheritance structure would be:
A A
\ /
B C
\ /
D
and not:
A
/ \
B C
\ /
D
(Usually, it is the latter you want, and when extending an
interface by inheritance, it's usually best to use virtual
inheritance.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34