Article View: net.lang.c++
Article #93Re: Function Overloading
From: Dataram Miruke
Date: Thu, 13 Mar 1986 16:21
Date: Thu, 13 Mar 1986 16:21
90 lines
3476 bytes
3476 bytes
> Assume the following declarations:
>
> int i;
>
> int f(int x)
> {
> // some code
> }
>
> overload f;
> float f(int x)
> {
> // some code
> }
>
>
> and the following call of function f:
>
> (void) f(i);
>
> Which function will be called, the one corresponding to int f(int x) or the
> one corresponding to float f(int x)?
>
> Bob Marti, Sun Microsystems
> ARPA: rmarti@sun.arpa
> UUCP: ... {decvax, hplabs, ihnp4, seismo, ucbvax}!sun!rmarti
I tried to compile the code fragmnent below and seems like our CC does
not like what it sees. this is what it returns which probably makes sense
considering what bs specifies about the function overloading in his book.
CC +L /u2/dmiruke/x.c:
"/u2/dmiruke/x.c", line 11: error: f redefined as overloaded
"/u2/dmiruke/x.c", line 13: warning: overloaded f() defined without being previously declared
1 error
(But then the CC version that we have is a pre-release version so.....!)
See page 290-291, in "The C++ Programming Language". In particular bs says ...
" When that name is used (overloaded name), the correct function is
selected by comparing the types of the actual arguments with the formal
argument types.
Finding which function to call is done in three steps :
1. Look for an exact match and use it if found.
2. Look for a match using the standard conventions and use any one if found.
3. Look for a match using user-defined conversions. If a unique set of
conversions is found then use it.
....... "
-- though the term "any one" in (2) above bothers me a bit. what if there
are more than one matches? how does the selection proceeds then? Maybe bs could
make it a bit clearer. below is the code fragment.
I believe that since the compiler uses the fact that the proper
selection amongst the overloaded functions is done with the help of argument
types it is important for the compiler to be able to differentiate between the
arguments at least to some extent. May be in the new version of c++ bs should
also consider the return type of the functions.
#include <stream.h>
int z = 5 ;
int f (int x)
{ cout << "from float f(int x) : " << x << "\n" ;
return x ;
}
overload f;
float f (int x)
{ cout << "from float f(int x) : " << x << "\n" ;
return (float) x ;
}
main ()
{
(void) f(z) ;
cout << "I love c++ except for what you are asking above " << "\n" ;
}
PS : (To bs specifically) : Have you considered adding the facility to let the
programmer define the new operators, rather than restricting him/er to use the
existing operators.
How about having a discussion on the net.lang.c++ about various
additions that people would like to see in the language.
- Dattaram T. Miruke
CSNET : dmiruke@udenva
dmiruke@isis
VOICE : 744-6045.
Technical Details
Message-ID:
<391@isis.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!decwrl!pyramid!hplabs!hao!nbires!isis!dmirukeReferences:
<3348@sun.uucp>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site isis.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!decwrl!pyramid!hplabs!hao!nbires!isis!dmiruke From: dmiruke@isis.UUCP (Dataram Miruke) Newsgroups: net.lang.c++ Subject: Re: Function Overloading Message-ID: <391@isis.UUCP> Date: Thu, 13-Mar-86 11:21:24 EST Article-I.D.: isis.391 Posted: Thu Mar 13 11:21:24 1986 Date-Received: Sat, 15-Mar-86 20:54:55 EST References: <3348@sun.uucp> Distribution: net Organization: University of Denver Math and Computer Science Lines: 90