-
January 15th, 2014, 02:47 PM
#1
Templates, multiple inheritance, and ambiguous overloads
Let's say I have a templated base class:
Code:
template <typename Input>
class Consumer
{
public:
void consume(Input input)
{
// do something
}
};
Now, I have a class which derives from this twice (A and B are unrelated, but otherwise unimportant classes):
Code:
class A;
class B;
class Derived: public Consumer<A>, public Consumer<B>
{
};
Now, elsewhere in the code I do this:
Code:
{
A myA;
Derived myDerived;
myDerived.consume(myA);
}
This looks like it should be simple enough, but g++ 4.8.1 is giving me a "request for member is ambiguous" error.
Any idea why?
-
January 15th, 2014, 03:59 PM
#2
Re: Templates, multiple inheritance, and ambiguous overloads
I suspect I need to add these to Derived:
Code:
using Consumer<A>::consume;
using Consumer<B>::consume;
After reviewing what I'm actually trying to do, though, I'm going to make Consumer a variadic class instead of using multiple inheritance.
-
January 16th, 2014, 05:00 AM
#3
Re: Templates, multiple inheritance, and ambiguous overloads
Originally Posted by Lindley
I suspect I need to add these to Derived:
Code:
using Consumer<A>::consume;
using Consumer<B>::consume;
Well I guess it means in this case overloading isn't enougth to separate methods and using is a way to overcome that.
I didn't know that and I probably would've added this in Derived:
Code:
void consume(A input) {Consumer<A>::consume(input);}
void consume(B input) {Consumer<B>::consume(input);}
so thank you.
Last edited by razzle; January 16th, 2014 at 07:35 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|