• Advertisement
Sign in to follow this  

Function Overloading Ambiguity

This topic is 3734 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've got a problem with function overloading in C++, I'd be grateful if anyone could help out. Say I have these two functions.... void SomeFunc( int& intbyref ); void SomeFunc( int intcopy ); How do I tell the compiler which one I want to use?

Share this post


Link to post
Share on other sites
Advertisement
Cast the argument to a constant integer. Then, only the second one will be usable.

Share this post


Link to post
Share on other sites
Quote:
Original post by nukomod
I've got a problem with function overloading in C++, I'd be grateful if anyone could help out. Say I have these two functions....


void SomeFunc( int& intbyref );
void SomeFunc( int intcopy );


How do I tell the compiler which one I want to use?


That's not a good design.
If those functions do different things then they should have different names.
If those functions do the same thing, but one changes the passed in value then it should be:

int SomeFunc (int intcopy);

Hoping that you'll remember in the future to cast the value to a const int to preserve the value will lead to bugs. Having the same name do two different things will also lead to confusion in the future.

Skizz

Share this post


Link to post
Share on other sites
Agreed. In general, having a single parameter of non-const-reference type is sketchy, especially if it's a primitive type.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement