C++/function overloading
Expert: Eddie - 6/1/2005
QuestionI have a problem using function overloading. I have declared the folowing classes/methods:
class Super{ };
class SubA:public Super { };
class SubB:public Super { };
static Class * Construct( const SubA * sub );
static Class * Construct( const SubB * sub );
and I try to call one of the methods with as argument a reference to a Super:
Super & s;
Class * c = Class::Construct( s ) ;
I got this error:
C2664: No constructor could take the source type, or constructor overload resolution was ambiguous
Do you see the problem?
greetings Matthieu
AnswerHello matthieu, thank you for the question.
Your problem is that your Super class has no idea that SubA and SubB derive from it. Your Construct function takes pointers to those respective types, and a Super isn't either one of them. The way that works is to declare your Construct function to take a Super pointer as a parameter, then you could pass either SubA or SubB because those classes know that they derive from Super.
Another error with that code would be that the Construct function takes a pointer as a parameter, and you are passing it a reference. You should be passing the address of that variable if it took a Super pointer.
If you have any other questions, please do not hesistate to ask.
I hope this information was helpful.
- Eddie