accessing base class in multiple inheritance

Discussion in 'C++' started by Robert Swan, Nov 30, 2004.

  1. Robert Swan

    Robert Swan Guest

    Given:

    struct a {
    int db;
    };

    class b: public a{};
    class c: public a{};

    struct d: public b, public c {
    int& dbb() {return b::db;}
    int& dbc() {return c::db;}
    };

    class e: public d{};
    class f: public d{};
    class g: public d{};

    class h:
    public e,
    public f,
    public g {
    void fine();
    void not_fine();
    };

    I'd like to know why g++ accepts

    void h::fine() {
    e::dbb() = 1;
    f::dbc() = 2;
    }

    but says that 'a' is an ambiguous base in

    void h::not_fine() {
    e::b::db = 1;
    f::c::db = 1;
    }

    Thanks,
    Robert
    Robert Swan, Nov 30, 2004
    #1
    1. Advertising

  2. Robert Swan wrote:
    > Given:
    >
    > struct a {
    > int db;
    > };
    >
    > class b: public a{};
    > class c: public a{};
    >
    > struct d: public b, public c {
    > int& dbb() {return b::db;}
    > int& dbc() {return c::db;}
    > };
    >
    > class e: public d{};
    > class f: public d{};
    > class g: public d{};
    >
    > class h:
    > public e,
    > public f,
    > public g {
    > void fine();
    > void not_fine();
    > };
    >
    > I'd like to know why g++ accepts
    >
    > void h::fine() {
    > e::dbb() = 1;
    > f::dbc() = 2;
    > }
    >
    > but says that 'a' is an ambiguous base in
    >
    > void h::not_fine() {
    > e::b::db = 1;
    > f::c::db = 1;
    > }


    It is an ambiguous base. Qualifying names doesn't help. In the
    end the compiler still tries to take "this" (h*) and convert it to
    a*, which it can't due to ambiguity.

    The only way to disambiguate is to tell which part of '*this' you
    intend on using:

    void h::not_fine() {
    static_cast<e*>(this)->b::db = 1;
    static_cast<f*>(this)->c::db = 1;
    }

    V
    Victor Bazarov, Nov 30, 2004
    #2
    1. Advertising

  3. Robert Swan

    Greg Schmidt Guest

    FWIW, VC7.1 seems to have no complaints about your example.

    --
    Greg Schmidt
    Trawna Publications http://www.trawna.com/
    Greg Schmidt, Nov 30, 2004
    #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. Ronnie
    Replies:
    4
    Views:
    420
    Ronnie
    Nov 19, 2003
  2. Ian
    Replies:
    7
    Views:
    4,422
    Chris Theis
    Feb 12, 2004
  3. Markus Bertheau

    Multiple inheritance with a common base class

    Markus Bertheau, Aug 10, 2004, in forum: Python
    Replies:
    0
    Views:
    311
    Markus Bertheau
    Aug 10, 2004
  4. Markus Bertheau
    Replies:
    5
    Views:
    618
    Duncan Booth
    Aug 10, 2004
  5. flopbucket
    Replies:
    2
    Views:
    414
    Andrey Tarasevich
    Jun 23, 2006
Loading...

Share This Page