pls abt templates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anushhprabu
    New Member
    • Sep 2006
    • 43

    pls abt templates

    how and where to use templates.. wat is size_t.. pls clearly with one small example ?? ??
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Use templates where you have a function or class that can be genralised for types

    i.e.

    Code:
    template<class T>
    T max(T a, T b)
    {
      if (a > b)
        return a;
    
      return b;
    }
    This is a generalised template function for max. That defines the logic without defining the types. It will for for any type for which > is defined (i.e. basic types and classes overloading the > operator).


    size_t is just an unsigned integer type that represents the size of something. It is returned by standard library functions like strlen and used as an input parameter in standadrd library functions like memcpy.

    Comment

    • anushhprabu
      New Member
      • Sep 2006
      • 43

      #3
      really thank you.. also can you tell where to use size_t ??

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        use size_t for any variable that represents the size of some other variable.

        Comment

        • anushhprabu
          New Member
          • Sep 2006
          • 43

          #5
          Originally posted by Banfa
          use size_t for any variable that represents the size of some other variable.
          thanks a lot..

          Comment

          Working...