+ 1

Asking about function overloading here.

What if I declare to functions : int sum(int a, int b = 0) and int sum(int a, float b =50) that just add a and b and return an int. What would be the result of sum(int 10) : 10 or 60?

25th Feb 2018, 6:45 PM
Etienne G
4 odpowiedzi
+ 6
This can be an example of undefined behaviour. There are a lot of undefined behaviors on c++ It's your responsibility to write good codes and avoid strange logics.. if it's not clear what your code will do it's simply won't be a good code. You should always strive for simply and readable codes, it will be good for clients (you often will be client of yourself)
25th Feb 2018, 8:13 PM
AZTECCO
AZTECCO - avatar
+ 3
This could depend on the language, compiler, version, and/or order of the code, the function name lookup table, in addition to whether or not the language has a type precedence for ambiguous function/method calls. This all results in the overload resolution. C++ http://en.cppreference.com/w/cpp/language/overload_resolution Will most likely result in a compile time error. Java http://javabypatel.blogspot.in/2016/05/ambiguous-method-overloading.html https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html
25th Feb 2018, 7:52 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
What language do you use ?
25th Feb 2018, 7:37 PM
sneeze
sneeze - avatar
+ 1
Ok guys I was just wandering while reading the cpp course. Undefined behavior looks bad, I'll make sure to avoid it! Thanks a lot, great community 🤘
25th Feb 2018, 8:39 PM
Etienne G