You are here:

C++/function overloading

Advertisement


Question
I 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  

Answer
Hello 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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2012 About.com, a part of The New York Times Company. All rights reserved.