|
-
July 18th, 2002, 11:19 AM
#1
Templates ?
HI
Can anyone give us a quick explanationof this subject
thanks
-
July 18th, 2002, 11:26 AM
#2
Templates provide a way to write functions/classes/methods generically. Meaning that they will work for different data types.
An example would be the best. Imagine a method that returns the absolute value of a double:
Code:
double abs(double d) { return d > 0.0 ? d : -d; }
This works great for doubles but not so well for other data types. The templatized version would look like:
Code:
template< class T >
T abs(T t) { return t > (T)0.0 ? t : -t; }
This will allow you to use abs with int, float, double, complex<>, or any class that behaves numerically (has the right operators).
There's a lot more to templates than this, but this is an easy example to start with.
Jeff
-
July 18th, 2002, 01:57 PM
#3
Take a look at one of my artical in which i discuss something about template. I disucss template only respect to ATL, although there are lots more you can do with template.
http://www.codeguru.com/atl/ATL_UndertheHood_3.html
Hope it helps.
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
|