GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Concepts  >  Data Structures

 Print  |  
Question:  what is the difference between function templates and function overloading?



August 08, 2008 01:03:03 #1
 efranford   Member Since: August 2008    Total Comments: 1 

RE: what is the difference between function templates and function overloading?
 

Function templates involve telling a function that it will be receiving a specified data type and then it will work with that at compile time.

The difference with this and function overloading is that function overloading can define multiple behaviors of function with the same name and multiple/various inputs.

eg:
Function Template

template<Class T>
void print(T var)
{
 cout << var;
}

The template T is basically passing an instance of a specified "Class" T into the function to work with.  With this you can really only use one Template and other inputs.  It is possible to overload a functions template.

Functoin Overloading
string ToString()
{
  return this;
}

string ToString(string)
{
  return this + string;
}

Notice how the second example is overloaded to allow the same name to be used while attaining a different response.
     

 

Back To Question