473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Jul 22 '05 #1
3 1196

"kazack" <ka****@talon.net> wrote in message
news:LX********************@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
Jul 22 '05 #2
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?
Jul 22 '05 #3
"kazack" <ka****@talon.net> wrote in message news:<LX********************@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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
12
by: Jack Daly | last post by:
I've inherited some code which uses an undocumented feature of a third-party vendor's library. Essentially, this vendor has kept the details of an interface struct secret, but we can pass a pointer...
10
by: Grizlyk | last post by:
1. Can abybody explain me why C++ function can not be overloaded by its return type? Return type can has priority higher than casting rules. For example: char input(); //can be compiled to name...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
4
by: king kikapu | last post by:
Hi, i am trying, to no avail yet, to take a C#'s overloaded functions skeleton and rewrite it in Python by using closures. I read somewhere on the net (http://dirtsimple.org/2004/12/python-is-...
1
by: bob | last post by:
I was just wondering if overloaded type cast functions must always be member functions of some class.
2
by: SlimT88 | last post by:
I recently created a simple class for a homework assignment and ran into compilation errors concerning overloaded global functions. I've copied the relevant parts of my code below and the errors...
9
by: Lawrence Krubner | last post by:
Possibly a dumb question but is type hinting allowed with overloaded methods? Can one class have these two methods? public function doSelect(Criteria $c) { // some code here } public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.