A
amit
hi,
am new to the group. So not sure of the topic has been discussed.....
I was reading about diamond shaped inheritance on wikipedia.
=======================
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 ?
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) !!
This kind of confused me. Is this true ?
am new to the group. So not sure of the topic has been discussed.....
I was reading about diamond shaped inheritance on wikipedia.
=======================
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 ?
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) !!
This kind of confused me. Is this true ?