ambiguous call to member

G

Guest

Hi, I am running into a problem with members which have the same name
in several base classes but different calling types and the comipler
complaining about amiguosity.

The VC++ docu list the following example:

struct A
{
void x(int i)
{
printf("\nIn A::x");
}
};

struct B
{
void x(char c)
{
printf("\nIn B::x");

}
};

struct C : A, B
{
// uncomment the following two lines to resolve this C2385
// using B::x;
// using A::x;
};

int main()
{
C aC;
aC.x(100); // C2385
aC.x('c'); // C2385
}

This also happens with types which have no valid conversions. Why that?
The compiler knows which type I am using, so why can't it choose the
correct member as it does if the two members of the same name are in
one class?

Second: The solution with the using directive given in the help does
not work if each base has several member of the same name like:

struct A
{
void x(type1);
void x(type2)
};

struct B
{
void x(type3)
void x(type4)
};

Any solution to that except using different names?

Thanks in advance for any comments.
Timm
 
P

Phlip

struct A
{
void x(int i)
{
printf("\nIn A::x");
}
};

struct B
{
void x(char c)
{
printf("\nIn B::x");

}
};

struct C : A, B
{
// uncomment the following two lines to resolve this C2385
// using B::x;
// using A::x;
};

int main()
{
C aC;
aC.x(100); // C2385
aC.x('c'); // C2385
}

A language lawyer will jump in, but I would guess that x and x do not
strictly "overload" each other, because they are not in the same scope. If
they were, their signatures (char and int, respectively) would distinguish
them.

Because they are in different scopes, both an equal distance from C, the
compiler declines to guess which one you mean.
This also happens with types which have no valid conversions. Why that?

If you were foolish or unlucky enough to name two things the same, in
production code, and if the compiler picked one, and if you refactored the
code, you would not want the compiler to change its guess and silently pick
the other one. Forcing you to declare which one you mean makes the code
more flexible.
Second: The solution with the using directive given in the help does
not work if each base has several member of the same name like:

struct A
{
void x(type1);
void x(type2)
};

struct B
{
void x(type3)
void x(type4)
};

Any solution to that except using different names?

Now they are in the same scope, and 'using' _doesn't_ distinguish them.

Use different names! That's a good style guideline in general. (I think it's
in the /Effective C++/ books.)
 
P

Pete Becker

Phlip said:
A language lawyer will jump in, but I would guess that x and x do not
strictly "overload" each other, because they are not in the same scope. If
they were, their signatures (char and int, respectively) would distinguish
them.
Yup.


Because they are in different scopes, both an equal distance from C, the
compiler declines to guess which one you mean.

Distance doesn't affect it. If two function names are defined in
different scopes they don't overload.
 
G

Guest

Distance doesn't affect it. If two function names are defined in
different scopes they don't overload.

I must say that I am still slightly confused because in simple
inheritance,
overloading of base class methods works. So that means that the
compiler adds scopes to a derived member if and only if it was defined
in more than one base classes?

Timm
 
P

Phlip

spam.baumeister said:
I must say that I am still slightly confused because in simple
inheritance,
overloading of base class methods works. So that means that the
compiler adds scopes to a derived member if and only if it was defined
in more than one base classes?

"Overload" only applies to one scope. Single inheritance can "hide" a method
in a parent class - that's neither overload nor "override".

In your first situation, the compiler cannot use overload lookup or scope to
pick a method, so it gives up. Like I said before, this is much better than
potentially surprising you, or your customers.
 
P

Pete Becker

I must say that I am still slightly confused because in simple
inheritance,
overloading of base class methods works. So that means that the
compiler adds scopes to a derived member if and only if it was defined
in more than one base classes?

struct base
{
void f(int) {}
};

struct derived : base
{
void f() {}
};

derived d;
d.f(1); // error
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top