Jan 21, 2009

In C++ and Java, overloaded functions(methods) must not differ just by the return type. Is it necessary or by design? What is the reason?

In C++ and Java, overloaded functions(methods) must not differ just by the return type. Is it necessary or by design? What is the reason?
It is necessary(No idea about java but in C++ it's so). Because the call to a function is bound to the function definition only by its signature i.e. its name, no. of parameters, type of parameters (and other factors like virtual function come into picture for dynamic binding but not the return value for sure).
However you can overload return type only with const keyword. When you return a reference and you do not want the reference changed in program, you should company the reference with a const.

No comments:

Post a Comment