0

In C++ can you have overloaded functions with the same parameter types but different return types?

The idea of overloaded functions is this int foo (int x, int y); int foo (double x, double y);

But will this work? int foo (int x, int y); double foo (int x, int y);

if the second way works, how does the compiler know which function to choose?

YelizavetaYR
  • 1,611
  • 6
  • 21
  • 37
  • 1
    No you can't. The parameter types are what C++ uses to determine which function to call. – Mark Ransom Sep 05 '14 at 16:57
  • You can't anyway. At least in C++. – Marco A. Sep 05 '14 at 16:58
  • 2
    The problem is that C++ is perfectly fine with you simply discarding values of function calls. What would happen if you did `foo(0,0);` as a statement by itself? Which function would get called? – scohe001 Sep 05 '14 at 16:59
  • The way to store it as an int vs a double after the function completes would be - to return value and probably return it with something higher hierarchically and then just demoted by setting it equal to an integer? – YelizavetaYR Sep 05 '14 at 16:59
  • 1
    @Josh - that was my question. My suspicious was that you couldn't for that very reason but I wanted to confirm. – YelizavetaYR Sep 05 '14 at 17:00

0 Answers0