472,977 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,977 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 13093
"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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.