accessing base class in multiple inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Swan

    #1

    accessing base class in multiple inheritance

    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
  • Victor Bazarov

    #2
    Re: accessing base class in multiple inheritance

    Robert Swan wrote:[color=blue]
    > 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;
    > }[/color]

    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

    Comment

    • Greg Schmidt

      #3
      Re: accessing base class in multiple inheritance

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

      --
      Greg Schmidt gregs@trawna.co m
      Trawna Publications http://www.trawna.com/

      Comment

      Working...