Multiple Inheritance

Discussion in 'C++' started by colint, Jul 20, 2006.

  1. colint

    colint Guest

    Hi
    I'm fairly new to c++ and I have a question regarding inheritance. I'm
    trying to create a class based on 2 inherited classes, e.g.

    class A
    {
    ...
    }

    class B: public A
    {
    ...
    }

    class C: public B
    {
    ...
    }

    The problem I'm having is how to ensure I have only 1 instance of class
    A, as at the moment I have 2 calls to the constructor/destructor of A
    for 1 instance of C. Will virtual inheritance solve this? I have tried
    class A
    {
    ...
    }

    class B: virtual public A
    {
    ...
    }

    class C: public B
    {
    ...
    }
    without success.
    Thanks
     
    colint, Jul 20, 2006
    #1
    1. Advertising

  2. colint wrote:
    > I'm fairly new to c++ and I have a question regarding inheritance. I'm
    > trying to create a class based on 2 inherited classes, e.g.
    >
    > class A
    > {
    > ..
    > }


    ;

    >
    > class B: public A
    > {
    > ..
    > }


    ;

    >
    > class C: public B
    > {
    > ..
    > }


    ;

    >
    > The problem I'm having is how to ensure I have only 1 instance of
    > class A, as at the moment I have 2 calls to the
    > constructor/destructor of A for 1 instance of C.


    REALLY? How do you know that? I don't see any c-tor/d-tor in A or
    in C.

    > Will virtual
    > inheritance solve this? I have tried class A
    > {
    > ..
    > }
    >
    > class B: virtual public A
    > {
    > ..
    > }
    >
    > class C: public B
    > {
    > ..
    > }
    > without success.


    No, virtual inheritance has nothing to do with it. Most likely
    your understanding is flawed, probably in the "1 instance of C"
    portion.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 20, 2006
    #2
    1. Advertising

  3. colint

    Rolf Magnus Guest

    colint wrote:

    > Hi
    > I'm fairly new to c++ and I have a question regarding inheritance. I'm
    > trying to create a class based on 2 inherited classes, e.g.
    >
    > class A
    > {
    > ..
    > }
    >
    > class B: public A
    > {
    > ..
    > }
    >
    > class C: public B
    > {
    > ..
    > }
    >
    > The problem I'm having is how to ensure I have only 1 instance of class
    > A,


    Neither B nor C will have two instances of A.

    > as at the moment I have 2 calls to the constructor/destructor of A
    > for 1 instance of C.


    I doubt that. Show how you found that out.
     
    Rolf Magnus, Jul 20, 2006
    #3
  4. colint

    mlimber Guest

    colint wrote:
    > Hi
    > I'm fairly new to c++ and I have a question regarding inheritance. I'm
    > trying to create a class based on 2 inherited classes, e.g.
    >
    > class A
    > {
    > ..
    > }
    >
    > class B: public A
    > {
    > ..
    > }
    >
    > class C: public B
    > {
    > ..
    > }
    >
    > The problem I'm having is how to ensure I have only 1 instance of class
    > A, as at the moment I have 2 calls to the constructor/destructor of A
    > for 1 instance of C.


    You shouldn't have a problem in the above code. I suspect you omitted
    something.

    > Will virtual inheritance solve this?
    > I have tried
    > class A
    > {
    > ..
    > }
    >
    > class B: virtual public A
    > {
    > ..
    > }
    >
    > class C: public B
    > {
    > ..
    > }


    See this FAQ and the one following on virtual inheritance:

    http://www.parashift.com/c -faq-lite/multiple-inheritance.html#faq-25.8

    But avoid inheritance if you don't need it. Multiple inheritance can
    often (but certainly not always) indicate design problems. You should
    use the weakest relationship between two classes that you can.
    Composition is a weak relationship; inheritance is a strong
    relationship.

    Cheers! --M
     
    mlimber, Jul 20, 2006
    #4
  5. colint

    colint Guest

    Rolf Magnus wrote:

    > colint wrote:
    >
    > > Hi
    > > I'm fairly new to c++ and I have a question regarding inheritance. I'm
    > > trying to create a class based on 2 inherited classes, e.g.
    > >
    > > class A
    > > {
    > > ..
    > > }
    > >
    > > class B: public A
    > > {
    > > ..
    > > }
    > >
    > > class C: public B
    > > {
    > > ..
    > > }
    > >
    > > The problem I'm having is how to ensure I have only 1 instance of class
    > > A,

    >
    > Neither B nor C will have two instances of A.
    >
    > > as at the moment I have 2 calls to the constructor/destructor of A
    > > for 1 instance of C.

    >
    > I doubt that. Show how you found that out.



    Ok, I just assumed it was to do with the multiple inheritance. Must be
    something else, thanks
     
    colint, Jul 20, 2006
    #5
  6. colint

    BigBrian Guest

    colint wrote:
    >Ok, I just assumed it was to do with the multiple inheritance. Must be
    > something else, thanks


    What multiple inheritance? There is no multiple inheritance in your
    sample code. Multiple inheritance would be like this...

    class C : public A, public B
    {

    };
     
    BigBrian, Jul 20, 2006
    #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. maxw_cc
    Replies:
    1
    Views:
    3,148
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    827
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,041
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,908
    Mike Schilling
    Feb 27, 2008
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    604
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page