By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

pls abt templates

P: 43
how and where to use templates.. wat is size_t.. pls clearly with one small example ?? ??
Sep 30 '06 #1
Share this Question
Share on Google+
4 Replies


Banfa
Expert Mod 5K+
P: 8,916
Use templates where you have a function or class that can be genralised for types

i.e.

Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. T max(T a, T b)
  3. {
  4.   if (a > b)
  5.     return a;
  6.  
  7.   return b;
  8. }
  9.  
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.
Sep 30 '06 #2

P: 43
really thank you.. also can you tell where to use size_t ??
Oct 4 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
use size_t for any variable that represents the size of some other variable.
Oct 4 '06 #4

P: 43
use size_t for any variable that represents the size of some other variable.
thanks a lot..
Oct 4 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.