Re: Overloaded functions
From: Stephen M. Webb (stephen.webb_at_bregmasoft.com)
Date: 11/27/03
- Next message: b83503104: "Need source code or free library (for UNIX) for JPG and GIF to BMP conversion"
- Previous message: Karl Heinz Buchegger: "Re: Parsing through a file and collect data ..."
- In reply to: kazack: "Overloaded functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 27 Nov 2003 08:18:53 -0800
"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
- Next message: b83503104: "Need source code or free library (for UNIX) for JPG and GIF to BMP conversion"
- Previous message: Karl Heinz Buchegger: "Re: Parsing through a file and collect data ..."
- In reply to: kazack: "Overloaded functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|