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.
 
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 ().
 
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.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top