|
-
Jul 18th, 2007, 08:07 AM
#1
Thread Starter
Addicted Member
[RESOLVED] can u say, what is the need of function overloading,even though there is templates?
Hi all!
What is the need of "function over loading" in c++? since C++ supports the "function templeates", which can enable the user to write only one generic function that can deal with different data types?
I think, to reduce the code for different funciton defnitions, "function templates" had been introduced.. am i right? or any advantage is there?
Thanks in advance:
regards:
raghunadhs.
-
Jul 18th, 2007, 08:13 AM
#2
Re: can u say, what is the need of function overloading,even though there is template
Method overloading and generic programming are two different concepts. Method overloading allows you to define multiple functions with the same name, but different call signatures. This allows you to effect optional parameters or specify some different behaviour depending on the type of arguments passed.
Code:
public:
int something(int foo, int bar);
int something(int foo, std::string bar);
int something(int foo, std::string bar, int abcdef);
Which exact method is called depends on the call signature that you use (the number and type of arguments—their names are irrelevant).
With templates, on the other hand, you do not specify the data type. This is for a different purpose.
You can mix the two concepts.
Last edited by penagate; Jul 18th, 2007 at 08:21 AM.
-
Jul 18th, 2007, 09:16 AM
#3
Thread Starter
Addicted Member
Re: can u say, what is the need of function overloading,even though there is template
Hi penagate!
That means can't we pass some arguements like std::stringbar ..etc to funciton templates? what my intention is, suppose if funciton templates do what the "function oveloading" does, then "funciton template"s are preferable... [in one interview i was faced this question, i said funciton templates are preferable, i don't know whether it is correct answer or not? this question may be useful to fresher guyes like me.. thats why i am stressing it]
Thanks in advance:
regards:
raghunadhs.
[code]public:
int something(int foo, int bar);
int something(int foo, std::string bar);
int something(int foo, std::string bar, int abcdef);
-
Jul 18th, 2007, 09:34 AM
#4
Re: can u say, what is the need of function overloading,even though there is template
Well the point is that they are used for different purposes.
A template allows you to substitute some arbitrary identifier, like 'T', in place of a type name, when defining a class or function. When you use the template in your code you must then specify a type name. The compiler will then compile one or more versions of the template depending on how many different types you use it with.
Take, for example, a vector: a resizable array. The data type that a vector holds is irrelevant to the algorithm; the concept is exactly the same no matter what you store in it. It would be tedious to have to write a vector class for every single data type in existence; hence, it is a template class.
Code:
vector<int> a_bunch_of_integers;
vector<someothertype> a_bunch_of_other_things;
Function templates are the same concept, except the genericity exists at the function level rather than the class level.
Function overloading really has nothing to do with templates. It exists because there are often situations where you may wish to pass a function different forms of data. Making multiple functions to do this is less convenient, because you then lose the implication that they serve the same purpose.
std::string::append has a whole bunch of different overloads, depending upon quite what exactly you wish to append to the string. The effect in each case is the same, though.
Does that help at all?
-
Jul 18th, 2007, 10:36 AM
#5
Thread Starter
Addicted Member
Re: can u say, what is the need of function overloading,even though there is template
Hi penagate!
Great clarification.. Thnk u very much. it will be helpful to me.
thanks:
regards:
raghunadhs.
[QUOTE=penagate]Well the point is that they are used for different purposes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|