the diamond problem

Discussion in 'C++' started by Tony Johansson, Aug 17, 2005.

  1. Hello Experts!

    It it correct to say that a solution to the diamond problem is to use
    virtual inheritance with virtual base classes.

    //Tony
     
    Tony Johansson, Aug 17, 2005
    #1
    1. Advertising

  2. Tony Johansson

    Pete Becker Guest

    Tony Johansson wrote:
    >
    > It it correct to say that a solution to the diamond problem is to use
    > virtual inheritance with virtual base classes.
    >


    It depends on what you mean by "the diamond problem." You use a virtual
    base when you need a base object that's shared among various derived
    objects. You don't use a virtual base when you don't want that.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Aug 17, 2005
    #2
    1. Advertising

  3. Tony Johansson

    Jaspreet Guest

    Tony Johansson wrote:
    > Hello Experts!
    >
    > It it correct to say that a solution to the diamond problem is to use
    > virtual inheritance with virtual base classes.
    >
    > //Tony


    Yeah, that is one of the most popular and efficient solutions to the
    diamond problem.

    class base
    {
    --
    };


    class der1:public base
    {
    --
    };

    class der2:public base
    {
    --
    }

    class sub_der:public der1, der2
    {
    --
    }

    Soo, now sub_der would have 2 copies of base, one from der1's side and
    the other from der2's side. To prevent this we can have the base class
    derived virtually.

    Just to add, a point which is OT here but in Java, this situation never
    arises since it does not support multiple inheritance. But again this
    point I said above is completely off-topic.
     
    Jaspreet, Aug 17, 2005
    #3
  4. Jaspreet wrote:
    > Just to add, a point which is OT here but in Java, this situation never
    > arises since it does not support multiple inheritance. But again this
    > point I said above is completely off-topic.


    Well, it could also be argued that this should not occur in C++ if you
    design your classes in a way that does not require it, but that's
    another day, another thread.
     
    Josh Mcfarlane, Aug 17, 2005
    #4
  5. Tony Johansson

    Pete Becker Guest

    Jaspreet wrote:

    >
    > Soo, now sub_der would have 2 copies of base, one from der1's side and
    > the other from der2's side. To prevent this we can have the base class
    > derived virtually.
    >


    And just to clarify: having 2 copies of base is not a "problem" in
    itself. It's a problem if it isn't what you want.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Aug 17, 2005
    #5
  6. Tony Johansson

    Guest

    and just to add, using virtual inheritance impacts the order of
    construction of your objects. It's a good interview question tied in
    with general questions about order of construction. Basically virtually
    inherited classes take precedence before non-virtually inherited
    classes. that's just FYI and one to watch if your interviewing. you
    can find out more in scott meyers effective c++ and various other
    books.


    G
     
    , Aug 18, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler

    virtual inheritance / dreaded diamond problem

    Alexander Stippler, Jul 14, 2003, in forum: C++
    Replies:
    0
    Views:
    1,884
    Alexander Stippler
    Jul 14, 2003
  2. "diamond" problem

    , Mar 1, 2007, in forum: C++
    Replies:
    14
    Views:
    834
    David Harmon
    Mar 1, 2007
  3. Pallav singh

    Template -- Diamond ring Problem

    Pallav singh, Dec 27, 2007, in forum: C++
    Replies:
    6
    Views:
    548
    Pavel Shved
    Dec 27, 2007
  4. thomas
    Replies:
    5
    Views:
    607
    Gert-Jan de Vos
    Nov 27, 2009
  5. sharmamandy
    Replies:
    0
    Views:
    1,016
    sharmamandy
    Sep 10, 2010
Loading...

Share This Page