
Overloading function prob with different return types
Quote:
> Assume the following function declarations
> void somefunc(CString, int, CString);
> int somefunc(CString, int, CString);
> and a function call like:
> int result = somefunc(tmpString1, 333, otherstring);
Well, the simple solution is to always return the value, and just ignore
it when it's not needed. This of course requires that the two version to
the same thing. However, if they do NOT do the same thing, then they should
have different names regardless of what they return.
OTOH, if you had two functions which perform essentially the same
function, by truly different only be their return value, there is a way to
do it, but it's usually more work than it's worth:
Defined a new class (which I'll call "somefunc_helper"). In this case,
it will probably need 3 member variables to match the parameters. Then
rewrite somefunc() (as a single function) which only filled a
somefunc_helper object with it's parameters, and returns the object.
Then write cast operators for somefunc_helper which do the actual work,
so the actions of "int somefunc(CString, int, CString)" are actually done in
somefunc_helper::operator int() (using the member variables instead of
parameters). Hence
Quote:
> int result = somefunc(tmpString1, 333, otherstring);
would be compiled as if it were:
somefunc_helper hlp = somefunc(tmpString1, 333, otherstring);
int result = (int) hlp;
while
Quote:
> float result = somefunc(tmpString1, 333, otherstring);
would be compiled as if it were:
somefunc_helper hlp = somefunc(tmpString1, 333, otherstring);
float result = (float) hlp;
(Warning ---- note that this means that the return value must actually be
assigned to something for the action actually to be done --- it cannot be
used as a void return)