Multiple inheritance ambiguity

D

Daniel Mitchell

struct PrivateBase { };
struct PublicBase { };

void foo( PrivateBase ) { }
void foo( PublicBase ) { }

struct Derived : PublicBase, private PrivateBase { };

int main()
{
Derived d;
foo( d ); // ambiguous
}


Why doesn't the call to foo( d ) resolve to foo( PublicBase )? Is this
a language defect?

D.
 
J

Jonathan Mcdougall

Daniel said:
struct PrivateBase { };
struct PublicBase { };

void foo( PrivateBase ) { }
void foo( PublicBase ) { }

struct Derived : PublicBase, private PrivateBase { };

int main()
{
Derived d;
foo( d ); // ambiguous
}


Why doesn't the call to foo( d ) resolve to foo( PublicBase )? Is this
a language defect?

This is by design.

[Member access control]
11§4 It should be noted that it is access to members and base classes
that is controlled, not their visibility. Names of members are still
visible, and implicit conversions to base classes are still considered,
when those members and base classes are inaccessible. The
interpretation of a given construct is established without regard to
access control. [...]


Jonathan
 
M

mlimber

Daniel said:
struct PrivateBase { };
struct PublicBase { };

void foo( PrivateBase ) { }
void foo( PublicBase ) { }

struct Derived : PublicBase, private PrivateBase { };

int main()
{
Derived d;
foo( d ); // ambiguous
}


Why doesn't the call to foo( d ) resolve to foo( PublicBase )? Is this
a language defect?

D.

Well, first of all you need references or pointers for your parameters
to foo(). But leaving that aside, Derived can be converted to a
PrivateBase by the members of Derived (see
http://www.parashift.com/c++-faq-lite/private-inheritance.html#faq-24.2),
so both versions of foo() need to participate in the overload
resolution in general. Consequently, you'll need to make your intention
explicit in the context of main().

Cheers! --M
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top