Template and classes

In my book, it describe template as a tool like a stencil. It is very generic and can become any data type. I know that it can become an int, float, etc. but can it also be an user created class?

1
2
3
4
5
6
7
8
template<class T>
int Compare (T a,T b)
{
   if(a==b)
      return true;//return true if the parameters are the same
   else
      return false;//return false if the parameters are different
}


So in the example above, I have a template call Compare and I can compare two values. Instead of doing this Compare(1,1); can I use my own user created class? For example,Compare(Date1,Date2); and if I can compare it, will I need to change my code for my class to be compatible?
Yes, you can. In your example only requirement for arguments, is that they should have operator== defined.
Okay thanks, I was confuse. I only need to overload operator==.
Topic archived. No new replies allowed.