Overloaded functions



 DEVELOP > c-Plus-Plus > Overloaded functions

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "kazack"
Date: 27 Nov 2003 05:09:31 AM
Object: Overloaded functions
I know that this is possible and I now how it is possible.
But why would you want to call more than one function the same name to begin
with? Wouldn't it be just easier to call them 2 different names?
Ex.
#include <string>
#include <iostream>
//why not integer_print and string_print??????
void print(int);
void print(string);
int main()
{
string name = "test";
int a = 4;
print(a);
print(name);
return 0;
}
void print(string name)
{
cout << name;
}
void print(int a)
{
cout << a;
}
Thanks for the understanding.
Shawn Mulligan
.

User: "Chris Theis"

Title: Re: Overloaded functions 27 Nov 2003 05:34:50 AM
"kazack" <kazack@talon.net> wrote in message
news:LXkxb.7429$Bv6.2253651@news1.epix.net...

I know that this is possible and I now how it is possible.
But why would you want to call more than one function the same name to

begin

with? Wouldn't it be just easier to call them 2 different names?

[SNIP]
IMHO it's easier for the user only to have to remember one name for
something which basically performs the same thing - in your example the
output of something. Why should the user have to care whether it's an
integer or a string or whatever. If you put this information into the name
of everything you'll end up with a whole lot of names that will puzzle the
user one day. It's a good rule that things that do the same thing, should
have the same name although they might operate on different objects/data
types and thus will have different parameters.
HTH
Chris
.

User: "Stephen M. Webb"

Title: Re: Overloaded functions 27 Nov 2003 10:18:53 AM
"kazack" <kazack@talon.net> wrote in message news:<LXkxb.7429$Bv6.2253651@news1.epix.net>...

I know that this is possible and I now how it is possible.
But why would you want to call more than one function the same name to begin
with? Wouldn't it be just easier to call them 2 different names?

Ex.
#include <string>
#include <iostream>
//why not integer_print and string_print??????
void print(int);
void print(string);

int main()
{
string name = "test";
int a = 4;
print(a);
print(name);
return 0;
}

void print(string name)
{
cout << name;
}

void print(int a)
{
cout << a;
}

It comes in particularly handy when used with operator overloading.
For example, you wanted to overload operator<< to handle, say, a
string and an int differently. You can't rename the operator, but you
can overload it for te two types.
As well, in C++, the types of the parameter form a part of the
function name (which may, in your view, be a chicken-and-egg type
problem). Adding the type to the name explicitly, as in print_string
and print_int, is redundant repetition of the same information.
Consider, also, the interaction of function overloading and templates.
If you could not overload a function, how could you template it?
--
Stephen M. Webb
.

User: "osmium"

Title: Re: Overloaded functions 27 Nov 2003 09:30:23 AM
kazack writes:

I know that this is possible and I now how it is possible.
But why would you want to call more than one function the same name to

begin

with? Wouldn't it be just easier to call them 2 different names?

All the good names get used up fast. Consider swap, abs, remove, .....
Would you really want to type 'interchange' for swap? What is the third guy
to come along going to use?
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER