Hi,
I have one doubt. Why virtual inheritance is required?
(i can achieve it by composition)
Is there any case where it is mandatory that the only way is virtual inheritance is required.
Thanks
Printable View
Hi,
I have one doubt. Why virtual inheritance is required?
(i can achieve it by composition)
Is there any case where it is mandatory that the only way is virtual inheritance is required.
Thanks
Virtual inheritance can be used to solve the Diamond problem which can occur when you use multiple inheritance.
The wikipedia page has an excellent explanation of this: http://en.wikipedia.org/wiki/Virtual_inheritance
That is my doubt..
Why that situation comes? In stead of doing deadly diamond design one can do the composition. I mean create the two private object of the two immediate parents. By this way we will not have the deadly diamond and control which ever function we want we can call.
If you do that, then you also lose the is-a relationship. No big deal if you're inheriting purely for the implementation, but inheritance of interface is a fairly common use of inheritance.Quote:
Originally Posted by Rajesh1978
Thanks,
in case of interface implementaion in the derived class I require that.
I'd say if a language allows multiple inheritance of implementation it must also supply virtual inheritance. They kind of go hand in hand. There must be a way to avoid the issues caused by diamond inheritance.
Some languages, such as Java, don't support multiple inheritance of implementation at all so it's perfectly possible not to use it in C++ too. You simply never publicly inherit implementation. Instead all base classes designed for public inheritance are always made pure virtual (interfaces). If you adhere to this policy you'll never need virtual inheritance. And it's even regarded good OO design.
i agree with nuzzle