Diamonds and redefinitions

Discussion in 'C++' started by John Doe, Feb 10, 2004.

  1. John Doe

    John Doe Guest

    I don't understand one thing about the diamond multiple inheritance. See
    the code:

    ------------------------------------------------------

    class A
    {
    public:
    void f() {std::cout("A");}
    };

    class B : virtual public A
    {
    public:
    void f() {std::cout("B");}
    };

    class C : virtual public A
    {
    };

    class D : public B, public C
    {
    };


    int main (.........) {
    D d;
    d.f();

    ........
    }

    ------------------------------------------------------

    This code compiles OK and the B::f() is called.


    BUT if I remove the virtual from the following lines:
    class B : virtual public A
    class C : virtual public A


    THEN it gives compile error:

    d:\Data\VC++7.1_projects\Prova1\Prova1.cpp(73) : error C2385: ambiguous
    access of 'f' in 'D'
    could be the 'f' in base 'B::f'
    or the 'f' in base 'A::f'
    d:\Data\VC++7.1_projects\Prova1\Prova1.cpp(73) : error C3861: 'f':
    identifier not found, even with argument-dependent lookup


    WHY this different behaviour???
     
    John Doe, Feb 10, 2004
    #1
    1. Advertising

  2. John Doe

    Guest Guest

    So, where is the problem???
    With your code you have this:

    A
    / \
    B C
    \ /
    D

    When you remove 'virtual', you have this:

    A A
    | |
    B C
    \ /
    D

    So, how A will be constructed? With B-path or with C-path??

    Apropos: You must use "virtual void f()" in class A (to have correct upcasting)

    > I don't understand one thing about the diamond multiple inheritance. See
    > the code:
    >
    > ------------------------------------------------------
    >
    > class A
    > {
    > public:
    > void f() {std::cout("A");}
    > };
    >
    > class B : virtual public A
    > {
    > public:
    > void f() {std::cout("B");}
    > };
    >
    > class C : virtual public A
    > {
    > };
    >
    > class D : public B, public C
    > {
    > };
    >
    >
    > int main (.........) {
    > D d;
    > d.f();
    >
    > .......
    > }
    >
    > ------------------------------------------------------
    >
    > This code compiles OK and the B::f() is called.
    >
    >
    > BUT if I remove the virtual from the following lines:
    > class B : virtual public A
    > class C : virtual public A
    >
    >
    > THEN it gives compile error:
    >
    > d:\Data\VC++7.1_projects\Prova1\Prova1.cpp(73) : error C2385: ambiguous
    > access of 'f' in 'D'
    > could be the 'f' in base 'B::f'
    > or the 'f' in base 'A::f'
    > d:\Data\VC++7.1_projects\Prova1\Prova1.cpp(73) : error C3861: 'f':
    > identifier not found, even with argument-dependent lookup
    >
    >
    > WHY this different behaviour???
     
    Guest, Feb 10, 2004
    #2
    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. Nut Cracker
    Replies:
    1
    Views:
    364
  2. jakk
    Replies:
    4
    Views:
    12,635
  3. biffta
    Replies:
    3
    Views:
    449
    biffta
    Sep 12, 2006
  4. titi

    if and and vs if and,and

    titi, Mar 9, 2007, in forum: VHDL
    Replies:
    4
    Views:
    627
    Mike Treseler
    Mar 11, 2007
  5. Immortal Nephi

    Relationship between multiple diamonds

    Immortal Nephi, Nov 7, 2008, in forum: C++
    Replies:
    2
    Views:
    324
    Immortal Nephi
    Nov 7, 2008
Loading...

Share This Page