Connecting Tech Pros Worldwide Forums | Help | Site Map

name collisions in multiple inheritance

Vam
Guest
 
Posts: n/a
#1: Jul 23 '05
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.



Vam
Guest
 
Posts: n/a
#2: Jul 23 '05

re: name collisions in multiple inheritance



"Vam" <ie@explorem.com> wrote in message
news:37cdglF59glb0U1@individual.net...[color=blue]
> 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.[/color]

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


Andrey Tarasevich
Guest
 
Posts: n/a
#3: Jul 23 '05

re: name collisions in multiple inheritance


Vam wrote:[color=blue]
> 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?[/color]

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.

--
Best regards,
Andrey Tarasevich
Closed Thread