Sometimes, I felt method overloading may create confusion.
class a {
public:
void copy(float f);
void copy(double d);
};
a me;
me.copy(1.2); // Not obvious at the first sight on which version we are calling.
A workaround on this is.
class a {
public:
void copyFloat(float f);
void copyDouble(double d);
};
However, having method with different name, to perform same functionality doesn’t seem a good idea as well. May I know, what do you consider, to choose among method overloading, or method with different naming?
Overloading for sure.
Okay, so it’s not “obvious” which function gets called (arguable)…so what? You don’t care that it can take different types of parameters, it just needs to do its thing. If you have different behavior based on different overloads, you’ve abused overloads, not pointed out a flaw in them.
An example of abusing overloads:
Hopefully you see the problem here. If two functions have the same name, they should do the same thing.
Even better, if you’re merely trying to allow the possibility for operating on different types, just use a template. (This arguably is a form of overloading.)