accessing base class in multiple inheritance

R

Robert Swan

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
 
V

Victor Bazarov

Robert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,037
Latest member
MozzGuardBugs

Latest Threads

Top