469,082 Members | 1,113 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,082 developers. It's quick & easy.

Multiple Inheritance Ambiguity

During a project I ran into trouble when using multiple inheritance. I
was able to resolve the problem, but was unable to really understand the
reasons for the error.

Here is a short example of the problem:

class BaseA
{
...

public:
bool X(char *s);

...
};

class BaseB
{
...

public:
bool X(int &i);

...
};

class AB : public BaseA, public BaseB
{
... /* Nothing special here, no overloading of X */
};

Now in the main function:

int main(int argc, char *argv[])
{
int MyInt;
AB MyAB;

MyAB.X(MyInt); /* Gives compile error */

return 0;
}

Note: I've added an example source to this post.

In the code all functions are implemented. When the code is compiled I
get the following error:

mi.cpp: In function `int main (int, char **)':
mi.cpp:45: request for member `X' is ambiguous
mi.cpp:33: candidates are: bool BaseB::X (int &)
mi.cpp:25: bool BaseA::X (char *)

It seems that the compiler cannot distinguish between the two X
functions. However, it should be clear that the BaseB::X function needs
to be used. Why can't the compiler determine this?

As I said before, I've solved the problem by putting X functions in the
AB class that invoke the correct X function in the BaseA or BaseB
classes. (Is this the best way to do this?)

Thanks in advance...
IvDČ

class BaseA
{
public:
BaseA();
bool X(char *s);
};

class BaseB
{
public:
BaseB();
bool X(int &i);
};

class AB : public BaseA, public BaseB
{
public:
AB();
};

BaseA::BaseA()
{}

bool BaseA::X(char *s)
{
return true;
}

BaseB::BaseB()
{}

bool BaseB::X(int &i)
{
return false;
}

AB::AB() : BaseA(), BaseB()
{}

int main(int argc, char *argv[])
{
int MyInt;
char *MyChar;
AB MyAB;

MyAB.X(MyChar); /* Gives compile error */

return 0;
}

Jul 22 '05 #1
5 12632
"IvDČ" <no**@none.com> wrote in message
news:cs**********@voyager.news.surf.net
During a project I ran into trouble when using multiple inheritance.
I was able to resolve the problem, but was unable to really
understand the reasons for the error.

Here is a short example of the problem:

class BaseA
{
...

public:
bool X(char *s);

...
};

class BaseB
{
...

public:
bool X(int &i);

...
};

class AB : public BaseA, public BaseB
{
... /* Nothing special here, no overloading of X */
};

Now in the main function:

int main(int argc, char *argv[])
{
int MyInt;
AB MyAB;

MyAB.X(MyInt); /* Gives compile error */

return 0;
}

Note: I've added an example source to this post.

In the code all functions are implemented. When the code is compiled
I get the following error:

mi.cpp: In function `int main (int, char **)':
mi.cpp:45: request for member `X' is ambiguous
mi.cpp:33: candidates are: bool BaseB::X (int &)
mi.cpp:25: bool BaseA::X (char *)

It seems that the compiler cannot distinguish between the two X
functions. However, it should be clear that the BaseB::X function
needs to be used. Why can't the compiler determine this?

As I said before, I've solved the problem by putting X functions in
the AB class that invoke the correct X function in the BaseA or BaseB
classes. (Is this the best way to do this?)

Thanks in advance...
IvDČ


This is by design. By default, overload resolution does not take place
across classes, because (the designers of C++ believed that) same-named
functions in different classes will typically have quite different purposes
and hence the selection between them should not be made on the basis of
their arguments.

If you want the two functions to be treated in the same way that they would
be if part of a single class, then the best way to do it is with using
declarations in the derived class. Just add

using BaseA::X;
using BaseB::X;

to the declaration of class AB.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
* =?ISO-8859-1?Q?IvD=B2?=:
This is a multi-part message in MIME format.
Please don't do that.

Here is a short example of the problem:

class BaseA
{
...

public:
bool X(char *s);

...
};

class BaseB
{
...

public:
bool X(int &i);

...
};

class AB : public BaseA, public BaseB
{
... /* Nothing special here, no overloading of X */
};

Now in the main function:

int main(int argc, char *argv[])
{
int MyInt;
AB MyAB;

MyAB.X(MyInt); /* Gives compile error */

return 0;
}

Note: I've added an example source to this post.

In the code all functions are implemented. When the code is compiled I
get the following error:

mi.cpp: In function `int main (int, char **)':
mi.cpp:45: request for member `X' is ambiguous
mi.cpp:33: candidates are: bool BaseB::X (int &)
mi.cpp:25: bool BaseA::X (char *)
This is per §10.2/2. If the name lookup (signatures are not yet
considered) yields declarations that are not all from subobjects of the
same type, there is ambiguity and the program is ill-formed. The simple
remedy, also described in that paragraph, is to use 'using' declarations
in class AB.

It seems that the compiler cannot distinguish between the two X
functions. However, it should be clear that the BaseB::X function needs
to be used. Why can't the compiler determine this?
That is a different question, "why is the language like this"? It's
best posted to [comp.std.c++]. I can't really see any reason why it's
like this -- it's very counter-intuitive.

As I said before, I've solved the problem by putting X functions in the
AB class that invoke the correct X function in the BaseA or BaseB
classes. (Is this the best way to do this?)


No, it isn't. See above.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3
IvDČ wrote:
During a project I ran into trouble when using multiple inheritance. I
was able to resolve the problem, but was unable to really understand the
reasons for the error.

Here is a short example of the problem:

class BaseA
{
...

public:
bool X(char *s);

...
};

class BaseB
{
...

public:
bool X(int &i);

...
};

class AB : public BaseA, public BaseB
{
... /* Nothing special here, no overloading of X */
};

Now in the main function:

int main(int argc, char *argv[])
{
int MyInt;
AB MyAB;

MyAB.X(MyInt); /* Gives compile error */

return 0;
}

Note: I've added an example source to this post.

In the code all functions are implemented. When the code is compiled I
get the following error:

mi.cpp: In function `int main (int, char **)':
mi.cpp:45: request for member `X' is ambiguous
mi.cpp:33: candidates are: bool BaseB::X (int &)
mi.cpp:25: bool BaseA::X (char *)

It seems that the compiler cannot distinguish between the two X
functions. However, it should be clear that the BaseB::X function needs
to be used. Why can't the compiler determine this?
The compiler is not trying to resolve overloaded
functions because these are *not* overloaded
functions. The standard is really clear on that
(see 10.2.2). If the same name is found in two
different bases, it's an ambiguity, even if they
could be resolved correctly with the call (i.e. in
your case). There are many good reasons not to
allow that, but here's one.

Imagine your class C derives from A and B and
these two base classes come from two different
libraires. If the author of B adds a new function
to the class, it may break the user's code by
redirecting a call from A::foo() to B::foo() if
the latter is a better match.
As I said before, I've solved the problem by putting X functions in the
AB class that invoke the correct X function in the BaseA or BaseB
classes. (Is this the best way to do this?)


If you want to have the functions from A and B
visible in C, yes. If not, you can select the
ones you want with 'using'.
Jonathan
Jul 22 '05 #4
> >> During a project I ran into trouble when using multiple inheritance.
I was able to resolve the problem, but was unable to really
understand the reasons for the error.

Here is a short example of the problem:

class BaseA
{
...

public:
bool X(char *s);

...
};

class BaseB
{
...

public:
bool X(int &i);

...
};

class AB : public BaseA, public BaseB
{
... /* Nothing special here, no overloading of X */
};

Now in the main function:

int main(int argc, char *argv[])
{
int MyInt;
AB MyAB;

MyAB.X(MyInt); /* Gives compile error */

return 0;
}

Note: I've added an example source to this post.

In the code all functions are implemented. When the code is compiled
I get the following error:

mi.cpp: In function `int main (int, char **)':
mi.cpp:45: request for member `X' is ambiguous
mi.cpp:33: candidates are: bool BaseB::X (int &)
mi.cpp:25: bool BaseA::X (char *)

It seems that the compiler cannot distinguish between the two X
functions. However, it should be clear that the BaseB::X function
needs to be used. Why can't the compiler determine this?

As I said before, I've solved the problem by putting X functions in
the AB class that invoke the correct X function in the BaseA or BaseB
classes. (Is this the best way to do this?)

Thanks in advance...
IvDČ

This is by design. By default, overload resolution does not take place
across classes, because (the designers of C++ believed that) same-named
functions in different classes will typically have quite different

purposes and hence the selection between them should not be made on the basis of
their arguments.

If you want the two functions to be treated in the same way that they would be if part of a single class, then the best way to do it is with using
declarations in the derived class. Just add

using BaseA::X;
using BaseB::X;

to the declaration of class AB.


Well, the C++ designers should have added an exception for templatized
classes.
If instead of BaseA and BaseB you had template <class T> BaseT (where
function
X() takes a T as a parameter), and class AB derived from BaseT<foo> and
BaseT<bar> then this argument that the functions don't represent the same
concept
falls away and you would like name resolution across multiple base classes
of the
same template type. I ran into this problem the hard way.
Jul 22 '05 #5
"Todd A. Anderson" <dr****@aaahawk.com.N0SPAM> wrote in message
news:cs**********@news01.intel.com

This is by design. By default, overload resolution does not take
place across classes, because (the designers of C++ believed that)
same-named functions in different classes will typically have quite
different purposes and hence the selection between them should not
be made on the basis of their arguments.

If you want the two functions to be treated in the same way that
they would be if part of a single class, then the best way to do it
is with using declarations in the derived class. Just add

using BaseA::X;
using BaseB::X;

to the declaration of class AB.


Well, the C++ designers should have added an exception for templatized
classes.
If instead of BaseA and BaseB you had template <class T> BaseT (where
function
X() takes a T as a parameter), and class AB derived from BaseT<foo>
and BaseT<bar> then this argument that the functions don't represent
the same concept
falls away and you would like name resolution across multiple base
classes of the
same template type. I ran into this problem the hard way.

It is not obvious that adding an exception to the usual rules makes the
language easier. Once you know what is required, adding using declarations
is an easy solution.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tony Johansson | last post: by
22 posts views Thread by Matthew Louden | last post: by
5 posts views Thread by Thomas Girod | last post: by
3 posts views Thread by ernesto | last post: by
14 posts views Thread by dl | last post: by
4 posts views Thread by Anarki | last post: by
47 posts views Thread by Larry Smith | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.