CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Templates ?

  1. #1
    Join Date
    May 2000
    Location
    England
    Posts
    574

    Templates ?

    HI
    Can anyone give us a quick explanationof this subject


    thanks

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  3. #3
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    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
  •  





Click Here to Expand Forum to Full Width

Featured