George2

Hello everyone,


I have learned public/private/protected inheritance, but what is virtual inheritance Here is the sample,

class Usable : public virtual Usable_lock

http://www.research.att.com/~bs/bs_faq2.html#no-derivation


thanks in advance,

George



Re: Visual C++ Language Virtual inheritance

einaros

Virtual inheritance ensures that the most derived class in a class hierarchy contains one and only one instance of virtually inherited classes up the tree. So if D derives from A in more than one branch up the tree, only one common instance will be kept at the bottom.





Re: Visual C++ Language Virtual inheritance

Pintu Shukla


When multiple inheritance is used.To avoid the duplicated base class , you should use the virtual keyword in the inheritance .

virtual inheritance

Thanx





Re: Visual C++ Language Virtual inheritance

George2

Thanks einaros,

einaros wrote:
Virtual inheritance ensures that the most derived class in a class hierarchy contains one and only one instance of virtually inherited classes up the tree. So if D derives from A in more than one branch up the tree, only one common instance will be kept at the bottom.

Very clear answer.

regards,

George





Re: Visual C++ Language Virtual inheritance

George2

Thanks Pintu,

Pintu Shukla wrote:

When multiple inheritance is used.To avoid the duplicated base class , you should use the virtual keyword in the inheritance .

virtual inheritance

Thanx

Good to learn from you again!

regards,

George





Re: Visual C++ Language Virtual inheritance

Pintu Shukla

Another way is to understand the concept is Following mention . have a look on the code.

Code Block

class A : public B
{
};

class C: public B
{
};


A Complication can be arise with multiple inheritance. if the Direct base classes are themselves derived from another class. The Possibilities arise here that both the base class could be derived from a common class. As we can see in above code.the duplication of the member of indirect base class can be confusing . and can aise a lots of problem. to avoid this problem we used virtual inheritance.


Specify the base as virtual instruct the compiler to make sure that the data member are not duplicated in the derived class Even if there are multiple occurence of the class as as indirect class.

Code Block

class A : public virtual B
{
};

class C: public virtual B
{
};



Thanx





Re: Visual C++ Language Virtual inheritance

George2

Thanks Pintu,

Good to learn from you again. I am clear now.

Pintu Shukla wrote:
Another way is to understand the concept is Following mention . have a look on the code.

Code Block

class A : public B
{
};

class C: public B
{
};


A Complication can be arise with multiple inheritance. if the Direct base classes are themselves derived from another class. The Possibilities arise here that both the base class could be derived from a common class. As we can see in above code.the duplication of the member of indirect base class can be confusing . and can aise a lots of problem. to avoid this problem we used virtual inheritance.


Specify the base as virtual instruct the compiler to make sure that the data member are not duplicated in the derived class Even if there are multiple occurence of the class as as indirect class.

Code Block

class A : public virtual B
{
};

class C: public virtual B
{
};



Thanx

regards,

George