CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    1

    Multiple inheritance and virtual functions.

    Hello everyone.

    I've recently been working with OO in C++ after switching from about a year of programming exclusively in Java.

    Since I have no experience with multiple inheritance I've been Googling trying to find an answer to my question but can't find one.

    Suppose I have classes A, B, C, D, and E.
    The hierarchy looks as follows:

    Code:
    A
    |
    |-----
    |      |
    B      C
    |      |
    |      |
    D      |
    |      |
    |----  |
           |
           E
    This is, B and C inherit from A, and D inherits from B, and E inherits from D and C.
    I understand this is a typical example of the "diamond" problem and that the best way to fix the ambiguity is through defining the parents of the subclass as 'virtual'.

    However, in this case, do I want to define only D and C as virtual, or D B and C as virtual, or just B and C as virtual?

    Thanks!

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Multiple inheritance and virtual functions.

    I found lots of results when I googled. This was third in the search.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Multiple inheritance and virtual functions.

    B and C should be virtual.
    Thanks for your help.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Multiple inheritance and virtual functions.

    It's good to know what to do, but in general I would avoid such diamond hierarchies.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jan 2009
    Posts
    19

    Re: Multiple inheritance and virtual functions.

    I think defining B and C as virtual would be a much better choice but this choice can differ depending on the structure and how u intend to use the varous classes .

    Keep in mind only one thing that if logic or features are going to change would your current declaraton will be able to adopt the change without having any effect on the control that you currently have on your code

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