What is the difference between function templates and function overloading?

Showing Answers 1 - 3 of 3 Answers

M.Senthil Cholan

  • Jul 21st, 2007
 

Function body will not differ in function Template, function body can differ in function overloading

Raj

  • Sep 15th, 2007
 

The function template has only one definition for various types.
But its not the case for the function over loading.

For eg.,

#include "stdafx.h"
#include "stdio.h"
#include "windows.h"
#include<iostream.h>
#include "test.h"


template < class T>
T add(T a, T b)
{
 T c;

 c= a+b;
 
 return c;
}


int main()
{

 cout << add<int>(1,2)<<endl;
 cout << add<float>(1.0,2.0)endl;


return 0;


}


But for the function overloading there has to be different datatypes defined for each functions.

bornToLose

  • Sep 22nd, 2007
 

Function overloading is declaring function with same names but different return, parameter and datatype. It is like normal function with same name


Function template is a generic type of function in which we provide a structure for function and its upto which how we used it for which type of data

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions