|
-
April 9th, 2012, 12:39 PM
#1
Virtual Inheritance
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
-
April 9th, 2012, 01:56 PM
#2
Re: Virtual Inheritance
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
-
April 9th, 2012, 09:42 PM
#3
Re: 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.
-
April 9th, 2012, 11:23 PM
#4
Re: Virtual Inheritance
 Originally Posted by Rajesh1978
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.
-
April 10th, 2012, 12:40 AM
#5
Re: Virtual Inheritance
Thanks,
in case of interface implementaion in the derived class I require that.
-
April 10th, 2012, 01:02 AM
#6
Re: Virtual Inheritance
 Originally Posted by Rajesh1978
That is my doubt..
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.
Last edited by nuzzle; April 10th, 2012 at 01:58 AM.
-
April 10th, 2012, 02:05 AM
#7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|