By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

questins about c++ templates

P: 2
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.......
Jun 6 '07 #1
Share this Question
Share on Google+
1 Reply


P: 21
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
:)
Jun 6 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.