CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Arrow 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

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    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

  3. #3
    Join Date
    Jul 2007
    Posts
    249

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Virtual Inheritance

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2007
    Posts
    249

    Re: Virtual Inheritance

    Thanks,
    in case of interface implementaion in the derived class I require that.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Virtual Inheritance

    Quote Originally Posted by Rajesh1978 View Post
    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.

  7. #7
    Join Date
    Jul 2007
    Posts
    249

    Re: Virtual Inheritance

    i agree with nuzzle

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured