Question

I understand that the overloading a function is legal if the number of parameters are different, or if the parameter type is different.

Why is it that a difference of only the return type is not legal?

Also, is it legal to overload as such:

int hello(int x);
int hello(int &z);

also

int hi(int x);
int hi(int *z);
Was it helpful?

Solution

There's no way for the compiler to figure out which function you're trying to call by just the return value. Since a return value does not need to be caught, you could write:

hello( 1 ); // return int
hello( 2 ); // return float

They are exactly the same call from what can be seen.

Yes that is legal since the first hi takes a reference and the second hi takes a memory address. You might call it like this:

hi( myNumber );
hi( &myNumber );

Perfectly distinguishable.

OTHER TIPS

It's hard for compiler to choose an overload based on the return type. In many situations there is no explicit statement for compiler to deduce the programmer's purposed return type. Even if it was possible, it's confusing for programmers.

Assume C++ has overloading based on return values:

float func() {...};
int func() {...};

int x = func(); // OK, compiler may choose `int` version!

BUT, How about...

cout << func() + func();

What do you expect? Maybe we can define some rules, but it will make codes complex, confusing and unreadable.

 

Other overloadings are legal. Due to different entry argument types.

You can't overload:

int hello(int x)   // 1
{
   return x;
}

int hello(int& x)  // 2
{
   return x;
}

int hello(int const& x) // 3
{
   return x;
}

void foo()
{
   int i = 10;
   hello(i);    // Can't disambiguate between 1 and 2
   hello(10);   // Can't disambiguate between 1 and 3
}

However, you can overload:

int hello(int& x)  // 4
{
   return x;
}

int hello(int const& x) // 5
{
   return x;
}

void foo()
{
   int i = 10;
   hello(i);    // Picks 4
   hello(10);   // Picks 5
}

Think about how the compiler might be able to figure out what function you want to be called. In the first example, if I write

int z = 5; 
int y = hello (z);

How is the compiler going to figure out which function you want to be called? But in the second example,

int z = 5; 
int y = hi (z);
int u = hi (&z);

it's clear which function to call. You can't overload on return type alone because the compiler needs the parameters to choose the function to call.

For me it's good, this is not the same type; for exemple int x is an integer, but int *z is un pointer of integer.

There are really no technical problems with allowing overloading on return type in many cases, the compiler knows what type is needed for the expression the function call is in and so knows which function to call. There are some problems though, for example if the returned value is not used, then you will have an ambiguity.

As for the functions, all of those are valid overloads. You might have some ambiguity in the case of hello depending on how you call it, for example if you call it with a variable then the compiler can't really tell which version of hello you want.

Since int, int& and int* are all different types then overloading on these is legal.

You can also overload on const which can be useful when designing classes which exhibit true const correctness.

int hello(int);
int hello(int) const;

You cannot overload on return types since the compiler would not know which function to use in full generality, especially if the function return type is discarded by the caller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top