hey guys i am studying c++ and i'd be thankfl if u cud anwer these qs for me
1. a template can be considered as a kind of a macro. then wat is d difference between them?
2.diff. btwn overloaded function and function templates
thnx.......
1.
I think why the template is considered to be kind of macro since both are resolved on compile time. As you know macros, i.e. preprocessing is done as the first step of the compilation.
While you consider about template they to replaced with the corresponding data type on the compile time.
i.e. Both macros and templates are done on compile time.
say a macro
#define MAX 14
void main()
{
int nData = MAX;
}
now on compile time the code will be, i.e. on obj will be
nData = 14;
Say a template function.
template < class T>
T AddNum( T Num1, T Num2 )
{
return ( Num1 + Num2 );
}
void main()
{
int nData = AddNum<int>(1,2);
double dData = AddNum<double>(1.333,2.333);
}
Now here the type of the data is resolved only on compile time.
That’s how you can relate them, but never misunderstand them.
Both templates and macro are different concepts.
Practically its not that good to use macros in c++.
2.
Overlaoded functions are the functions with same name and different arguments,
i.e same name and different signature.
Say
Add(int ndata, char cdata );
Add(int nData, long ldata, char cdata );Etc…..
While templates are functions which will resolve its type on compile time.same as the examples given above.
Hope u got the point
:)