Overloading function prob with different return types 
Author Message
 Overloading function prob with different return types

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);

The compiler is not able to recognize the appropriate function (the
second).He complains: "...ambigous call to function somefunc..."

Is there a workaround for this or an efficient way of letting the
compiler know which function to use (without creating a dummy parameter
or renaming one of the two functions) ?

Thanx

Monica

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 26 Apr 2002 03:00:00 GMT  
 Overloading function prob with different return types
you have to create a dummy parameter.  Overloading a function by return type
is not valid C++ code.  Actually I believe it did make it into the final C++
standard, but I'm not aware of any compiler that supports it

--
Visit: http://ourworld.compuserve.com/homepages/jjmarshall/



Fri, 26 Apr 2002 03:00:00 GMT  
 Overloading function prob with different return types

Quote:

> you have to create a dummy parameter.  Overloading a function by return type
> is not valid C++ code.  Actually I believe it did make it into the final C++
> standard, but I'm not aware of any compiler that supports it

It didn't make it into the Standard nor was it ever suggested.

What you're referring to is (probably) return type covariance, which is
applicable only to virtual member functions of a base class whose return
type may co-vary with the type of their class in a derived class. VC++
doesn't support this feature yet.
However, the original posting referred to a different situation --
overloading a global function. This is not permitted in C++.

Danny Kalev

"The ANSI/ISO C++ Professional Programmer's Handbook"
http://www.amazon.com/exec/obidos/ASIN/0789720221
http://www.devx.com/free/books/bookreview.asp?bookid=286

Quote:

> --
> Visit: http://ourworld.compuserve.com/homepages/jjmarshall/



Sat, 27 Apr 2002 03:00:00 GMT  
 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)



Sat, 27 Apr 2002 03:00:00 GMT  
 Overloading function prob with different return types
If you still need to use the same function name with different return types,
why don't you declare them in a namespace, and then you could refer each of
them with their respective namespaces.



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

> > 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

> > 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)



Sat, 27 Apr 2002 03:00:00 GMT  
 Overloading function prob with different return types
I stand corrected - could have sworn I heard that - anyway thanks for the
info

--
Visit: http://ourworld.compuserve.com/homepages/jjmarshall/



Sun, 28 Apr 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Overloading function prob with different return types

2. Virtual Function with Different Return Type

3. localtime function returns different values for different winnt operating systems

4. Inheritance, overloading and return type

5. Function pointer pointing to a different function type

6. Explicit interface members with different return types in MC++

7. Returning different types in ATL (.NET)

8. Make inherited methods return different types

9. virtuality with different return type

10. Overload Function Return Value

11. Type of function returning function.

12. Help - Calling ActiveX DLL function from VB and C++ returns different values

 

 
Powered by phpBB® Forum Software