Multiple ineritance question

Discussion in 'C++' started by Mattias B, Oct 28, 2003.

  1. Mattias B

    Mattias B Guest

    Hello!

    I have s quick question regarding multiple ineritance that I hope that
    someone will have the time to look at. I have the following classes that
    I try to compile and use:

    class A
    {
    public:
    virtual void foo() = 0;
    };

    class B : public A
    {
    public:
    virtual void bar() = 0;
    };

    class A_Impl : public A
    {
    public:
    void foo() { }
    };

    class B_Impl : public A_Impl, public B
    {
    public:
    void bar() { }
    };

    Now when I try to create an instance of B_Impl I get the following error
    message when I try to compile with gcc:

    main.cpp: In function `int main()':
    main.cpp:7: cannot allocate an object of type `B_Impl'
    main.cpp:7: because the following virtual functions are abstract:
    A.h:7: virtual void A::foo()

    Can anyone tell me why this is? Surely B_Impl has inerited A_Impl's
    implementation of void foo()?

    Regards,
    Mattias
     
    Mattias B, Oct 28, 2003
    #1
    1. Advertising

  2. Mattias B wrote:
    > ...
    > I have s quick question regarding multiple ineritance that I hope that
    > someone will have the time to look at. I have the following classes that
    > I try to compile and use:
    >
    > class A
    > {
    > public:
    > virtual void foo() = 0;
    > };
    >
    > class B : public A
    > {
    > public:
    > virtual void bar() = 0;
    > };
    >
    > class A_Impl : public A
    > {
    > public:
    > void foo() { }
    > };
    >
    > class B_Impl : public A_Impl, public B
    > {
    > public:
    > void bar() { }
    > };
    >
    > Now when I try to create an instance of B_Impl I get the following error
    > message when I try to compile with gcc:
    >
    > main.cpp: In function `int main()':
    > main.cpp:7: cannot allocate an object of type `B_Impl'
    > main.cpp:7: because the following virtual functions are abstract:
    > A.h:7: virtual void A::foo()
    >
    > Can anyone tell me why this is? Surely B_Impl has inerited A_Impl's
    > implementation of void foo()?
    > ...


    'B_Impl' has two subobjects of type 'A'. One comes through base class
    subobject 'A_Impl', another - through base class subobject 'B'. Virtual
    function 'foo' from the former has 'A_Impl::foo' as final overrider,
    which is not pure. Virtual function 'foo' from the latter has 'A::foo'
    as final overrider, which is _pure_. For this reason, as defined in
    10.4/4 class 'B_Impl' is an abstract class and cannot be instantiated.

    Provide a non-pure final overrider for all 'foo' functions is your class
    'B_Impl' and the code will compile. If I understand your intent
    correctly, you need something like this

    class B_Impl : public A_Impl, public B
    {
    public:
    void bar() {}
    void foo() { A_Impl::foo(); }
    };

    You should probably also decide whether you really need two distinct
    base class subobjects of type 'A' in 'B_Impl' and, maybe, use 'A' as
    virtual base class.

    --
    Best regards,
    Andrey Tarasevich
    Brainbench C and C++ Programming MVP
     
    Andrey Tarasevich, Oct 28, 2003
    #2
    1. Advertising

  3. Mattias B

    jeffc Guest

    "Mattias B" <> wrote in message
    news:3f9e20a2$0$166$...
    > Hello!
    >
    > I have s quick question regarding multiple ineritance that I hope that
    > someone will have the time to look at. I have the following classes that
    > I try to compile and use:
    >
    > class A
    > {
    > public:
    > virtual void foo() = 0;
    > };
    >
    > class B : public A
    > {
    > public:
    > virtual void bar() = 0;
    > };
    >
    > class A_Impl : public A
    > {
    > public:
    > void foo() { }
    > };
    >
    > class B_Impl : public A_Impl, public B
    > {
    > public:
    > void bar() { }
    > };
    >
    > Now when I try to create an instance of B_Impl I get the following error
    > message when I try to compile with gcc:
    >
    > main.cpp: In function `int main()':
    > main.cpp:7: cannot allocate an object of type `B_Impl'
    > main.cpp:7: because the following virtual functions are abstract:
    > A.h:7: virtual void A::foo()
    >
    > Can anyone tell me why this is? Surely B_Impl has inerited A_Impl's
    > implementation of void foo()?


    Yes, but not B's. You're inheriting "doubly" from A. Based on the fact
    that you're asking this question leads me to think that you probably
    actually want virtual inheritance from A.
     
    jeffc, Oct 28, 2003
    #3
    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. Stephen Miller
    Replies:
    0
    Views:
    1,653
    Stephen Miller
    Jun 24, 2003
  2. Andrew Kidd
    Replies:
    3
    Views:
    482
    Andrew Kidd
    Apr 22, 2004
  3. Diane Selby
    Replies:
    4
    Views:
    6,788
  4. tshad
    Replies:
    2
    Views:
    472
    tshad
    Jan 27, 2005
  5. Schultz
    Replies:
    1
    Views:
    515
    Karl Seguin
    Feb 14, 2005
Loading...

Share This Page