473,425 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

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 13138
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello Experts! I just play around just to try to understand this about multiple inheritance. You have all the class definition below and at the bottom you have the main program. So here I...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
5
by: Thomas Girod | last post by:
Hi. I think I'm missing something about multiple inheritance in python. I've got this code. class Foo: def __init__(self): self.x = "defined by foo" self.foo = None
3
by: ernesto | last post by:
Hi everybody I have the following class declarations: class Interface { public: virtual char* getName() const = 0; }; class BaseClass : public Interface {
14
by: dl | last post by:
I have two classes, say A and B, both having a data member 'int n'; private in A, public in B. When I derive class C from both public A and public B, B::n should be visible to C while A::n...
4
by: Anarki | last post by:
##include <iostream> using namespace std; class A { }; class B:virtual public A { }; class C:virtual public A
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
21
by: raylopez99 | last post by:
Well, contrary to the implication in my 2000 textbook on C# (public beta version), C# does allow multiple inheritance, so long as it's serially chained as follows: class derived02 : derived01 {...
3
by: Adam Nielsen | last post by:
Hi everyone, I've run into yet another quirk with templates, which IMHO is a somewhat limiting feature of the language. It seems that if you inherit multiple classes, and those classes have...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.