HI
Can anyone give us a quick explanationof this subject
thanks
Printable View
HI
Can anyone give us a quick explanationof this subject
thanks
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:
This works great for doubles but not so well for other data types. The templatized version would look like:Code:double abs(double d) { return d > 0.0 ? d : -d; }
This will allow you to use abs with int, float, double, complex<>, or any class that behaves numerically (has the right operators).Code:template< class T >
T abs(T t) { return t > (T)0.0 ? t : -t; }
There's a lot more to templates than this, but this is an easy example to start with.
Jeff
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.