Click to See Complete Forum and Search --> : Templates ?


posty68
July 18th, 2002, 11:19 AM
HI
Can anyone give us a quick explanationof this subject


thanks

jfaust
July 18th, 2002, 11:26 AM
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:


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:


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

Zeeshan
July 18th, 2002, 01:57 PM
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.