The return type is not a reason for overloading functions.
Function overloading can be performed with only one of the following criteria:
- No argument
- Argument Type &
- Sequence of arguments
The return type can be ignored by the caller and therefore is not an acceptable criterion for function overloading.
Having said the above, go over the value and passing the link will create ambiguity for the compiler. For instance,
void doSomething(int i) { } void doSomething(int &i) { } int main() { int val = 10; doSomething(val);
Here, the compiler cannot determine how to pass val
to the doSomething()
version. It can make a valid function call for any version, so it asks for help at compile time (since this is a static binding) and puts the calls as ambiguous.
In the case of, for example, you. It is a choice / preference to rename functions or pass a pointer argument that will make two functions overloaded (one name, but different types of arguments). However, it is important to take into account the requirement and the action that the function will perform when choosing a preference. Personally, I would not choose a pointer just for the sake of overloading. If I need to reinstall or indicate that my argument points to different variables, then it would be wise to select a pointer argument.
A simple way is to simply have two different function names. There is no overhead, and it is as efficient as any other function call.
source share