vendredi 8 août 2014

Différence entre le virtuel et simple héritage en c ++ - Stack Overflow



This question already has an answer here:




What is the difference between


Class A {};
Class Z: public A {};

and


Class A {};
Class Z: virtual public A{};



Assuming that there is no additional inheritance hierarchy, there is no difference in this case. The only way to see a difference is inheriting the same class through multiple ways - for example, as follows:


class ZA : public Z, public A {};

vs.


class ZA : virtual public Z, virtual public A {};

In the first case, ZA would have two regions with separate As - one inherited directly, and one inherited through Z. In the second case, there would be only one A, inherited through both paths, and shared.


Here is an illustration of this:


Virtual vs. Regular inheritance




The difference is in the behaviour when you inherit from multiple classes which share a common base class:


class A {};
class Y : public A {};
class Z : public A {};
class YZ: public Y, public Z {};

In this case, with non-virtual inheritance, a YZ object would contain Y and Z subobjects, each with their own A subobject.


This is usually not what you want, as it doesn't follow the "is-a" relationship that inheritance usually models: there are two different ways in which a YZ can be viewed as an A.


void f(A&);
YZ yz;
f(yz); // ERROR: which A?

But with virtual inheritance of A, there will be only one A subobject, shared by the Y and Z; the example above will now be unambiguous.




Suppose you have two derived classes B and C that have a common base class A. And you have another class D that inherits both of them. You would use public virtual A to ensure that both B and C use the same subobject A.


Info taken from here: Virtual Base Classes




This question already has an answer here:




What is the difference between


Class A {};
Class Z: public A {};

and


Class A {};
Class Z: virtual public A{};


Assuming that there is no additional inheritance hierarchy, there is no difference in this case. The only way to see a difference is inheriting the same class through multiple ways - for example, as follows:


class ZA : public Z, public A {};

vs.


class ZA : virtual public Z, virtual public A {};

In the first case, ZA would have two regions with separate As - one inherited directly, and one inherited through Z. In the second case, there would be only one A, inherited through both paths, and shared.


Here is an illustration of this:


Virtual vs. Regular inheritance



The difference is in the behaviour when you inherit from multiple classes which share a common base class:


class A {};
class Y : public A {};
class Z : public A {};
class YZ: public Y, public Z {};

In this case, with non-virtual inheritance, a YZ object would contain Y and Z subobjects, each with their own A subobject.


This is usually not what you want, as it doesn't follow the "is-a" relationship that inheritance usually models: there are two different ways in which a YZ can be viewed as an A.


void f(A&);
YZ yz;
f(yz); // ERROR: which A?

But with virtual inheritance of A, there will be only one A subobject, shared by the Y and Z; the example above will now be unambiguous.



Suppose you have two derived classes B and C that have a common base class A. And you have another class D that inherits both of them. You would use public virtual A to ensure that both B and C use the same subobject A.


Info taken from here: Virtual Base Classes


0 commentaires:

Enregistrer un commentaire