Multiple Inheritance Ambiguity

  • Thread starter =?ISO-8859-1?Q?IvD=B2?=
  • Start date
?

=?ISO-8859-1?Q?IvD=B2?=

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;
}
 
J

John Carson

IvD² said:
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.
 
A

Alf P. Steinbach

* =?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.
 
J

Jonathan Mcdougall

IvD² said:
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
 
T

Todd A. Anderson

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.
 
J

John Carson

Todd A. Anderson said:
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.
 

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

Latest Threads

Top