473,668 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple inheritance/overloading ambiguity - is this behaviour intentional?

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
two functions with the same name, this is an error - even when the
functions take different parameters, and they would happily exist as
overloaded functions if they were declared in the same class instead of
being inherited.

For example, this code won't compile:

class A { };
class B { };

template <typename T>
class Parent
{
public:
void doit(const T& t)
{
// do stuff with t
}
};

class Child: public Parent<A>, public Parent<B>
{
};

int main(void)
{
A a;
B b;
Child c;
c.doit(a); // call Parent<A>.doit( )
c.doit(b); // call Parent<B>.doit( )
return 0;
}

My understanding is that the compiler must find a unique name *before*
it looks at overloading, which means the above code fails because doit()
exists twice, even though each version takes a different parameter type.

I'm surprised this seems to be the intended behaviour (I thought it was
a bug in my compiler.) I can't think of any reason where this would be
desired behaviour!

I've come up with a workaround, which involves implementing a single
function in class Child which resolves the ambiguity:

class Child: public Parent<A>, public Parent<B>
{
public:
template <class T>
void doit2(const T& t)
{
this->Parent<T>::doi t(t);
}
};

This fixes the problem, and avoids the need to manually specify the type
in main() where the users of my class aren't supposed to know about the
type (not to mention the typename it's very long and makes the code look
messy.)

Is this a correct way of dealing with this issue? Are there any better
ways?

Thanks,
Adam.
Sep 26 '07 #1
3 2138
Adam Nielsen wrote:
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
two functions with the same name, this is an error - even when the
functions take different parameters, and they would happily exist as
overloaded functions if they were declared in the same class instead
of being inherited.

For example, this code won't compile:

class A { };
class B { };

template <typename T>
class Parent
{
public:
void doit(const T& t)
{
// do stuff with t
}
};

class Child: public Parent<A>, public Parent<B>
{
Just add

using Parent<A>::doit ;
using Parent<B>::doit ;

here and everything compiles.
};

int main(void)
{
A a;
B b;
Child c;
c.doit(a); // call Parent<A>.doit( )
c.doit(b); // call Parent<B>.doit( )
return 0;
}

My understanding is that the compiler must find a unique name *before*
it looks at overloading, which means the above code fails because
doit() exists twice, even though each version takes a different
parameter type.
I'm surprised this seems to be the intended behaviour (I thought it
was a bug in my compiler.) I can't think of any reason where this
would be desired behaviour!

I've come up with a workaround, which involves implementing a single
function in class Child which resolves the ambiguity:

class Child: public Parent<A>, public Parent<B>
{
public:
template <class T>
void doit2(const T& t)
{
this->Parent<T>::doi t(t);
}
};

This fixes the problem, and avoids the need to manually specify the
type in main() where the users of my class aren't supposed to know
about the type (not to mention the typename it's very long and makes
the code look messy.)

Is this a correct way of dealing with this issue? Are there any
better ways?
See above.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 26 '07 #2
On Sep 26, 8:17 am, Adam Nielsen <adam.niel...@r emove.this.uq.e du.au>
wrote:
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
two functions with the same name, this is an error - even when the
functions take different parameters, and they would happily exist as
overloaded functions if they were declared in the same class instead of
being inherited.

For example, this code won't compile:

class A { };
class B { };

template <typename T>
class Parent
{
public:
void doit(const T& t)
{
// do stuff with t
}
};

class Child: public Parent<A>, public Parent<B>
{
public:
using Parent<A>::doit ;
using Parent<B>::doit ;
};


Sep 26 '07 #3
Just add
>
using Parent<A>::doit ;
using Parent<B>::doit ;

here and everything compiles.
Ah, I knew there'd be an easier way, thanks for that. Unfortunately it
won't be the tidiest solution for my problem - if each class inherits a
bunch of functions, that could be quite a lot of "using" statements.

I've come up with somewhat of a hack to get around it, in the form of a
new class that will only be inherited once:

template <class TMain>
class ParentHandler
{
public:
template <class TParent>
void doit2(const TParent& param)
{
TMain *p = static_cast<TMa in *>(this);
return p->Parent<TParent >::doit(param );
}
};

It can then be used like this:

class Child: public Parent<A>, public Parent<B>,
public ParentHandler<C hild>
{
};

And then it removes the ambiguity when used, regardless of how many
Parent classes are inherited (and no code changes are required when
adding more inherited objects):

c.doit2(a); // call Parent<A>.doit( )
c.doit2(b); // call Parent<B>.doit( )

Granted it's somewhat ugly, but my emphasis here is to make my class
easy to use, not necessarily easy to write :-)

Cheers,
Adam.
Sep 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3476
by: zbyszek | last post by:
I am working with a large C++ program which, for reasons of backward compatibility, uses C's printf and fprintf rather than iostreams. For a certain type of build I want to provide new functions that change the behaviour of printf and fprintf, and I planned to make them available using a convenient namespace, called 'herring', say, like this: using herring::printf; using herring::fprintf;
5
13152
by: IvD² | last post by:
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 { ...
12
2967
by: RA Scheltema | last post by:
Hi all, I have the following code: namespace A { inline void func(int) { ...; } inline void func(float) { ...; } inline void func(char) { ...; } }
5
2107
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 use multiple inheritance. One top class called MBase and below this we have D1 and D2 on the same level.
9
1653
by: Karahan Celikel | last post by:
Here are three simple classes: class A { public void DoIt(B b) { DoSomething(b); } public void DoSomething(B b) {
31
2274
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
3
2645
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 {
47
4007
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 support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
21
2475
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 { } class derived01 : baseclass01
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8889
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8650
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2781
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.