name collisions in multiple inheritance


V

Vam

my compiler doesn't compile the following:

struct Base1
{
virtual void F(){}
};

struct Base2
{
virtual void F(){}
};

struct Derived : public Base1, private Base2
{
}

int main()
{
Derived d;
d.F();

return 0;
}

It complains that F is ambiguous. But when I try to do

d.Base2::F();

it notices that Base2 is private and hence inaccessible. Is my compiler
broken in this regard? I know I can use 'using base1::F()' in my derived
class, or 'd.Base1::F()' in main(), but it surprises me that I have to.
 
Ad

Advertisements

V

Vam

Vam said:
my compiler doesn't compile the following:

struct Base1
{
virtual void F(){}
};

struct Base2
{
virtual void F(){}
};

struct Derived : public Base1, private Base2
{
}

int main()
{
Derived d;
d.F();

return 0;
}

It complains that F is ambiguous. But when I try to do

d.Base2::F();

it notices that Base2 is private and hence inaccessible. Is my compiler
broken in this regard? I know I can use 'using base1::F()' in my derived
class, or 'd.Base1::F()' in main(), but it surprises me that I have to.

Just some minor corrections, add a ; after the Derived struct declaration,
and the 'using base1::F' shouldn't have the trailing ().
 
Ad

Advertisements

A

Andrey Tarasevich

Vam said:
my compiler doesn't compile the following:

struct Base1
{
virtual void F(){}
};

struct Base2
{
virtual void F(){}
};

struct Derived : public Base1, private Base2
{
}

int main()
{
Derived d;
d.F();

return 0;
}

It complains that F is ambiguous. But when I try to do

d.Base2::F();

it notices that Base2 is private and hence inaccessible. Is my compiler
broken in this regard?

No. In C++ access specifications have no effect on name resolution. The
"privateness" of 'Base2::F' does not prevent it from participating in
name resolution process. In other words, the compiler first tries to
resolve the name and only then starts to pay attention to the access
rights. In your case name 'F' is indeed ambiguous.
 

Top