转自:http://blog.sina.com.cn/s/blog_913c070a0100zuah.html
还是例子说话。
函数模板:
template<class T> max(const T a, const T b)
{
}
这样用:
//int
int a = 1, b = 2;
max(a, b);
//float
float c = 1.0f, d = 2.0f;
max(c, d);
//double
double e = 1.0, f = 2.0;
max(e, f);
//...
类模板:
template<class T>
class C
{
public:
};
对这个类进行实例化:
//int
C<int> cn;
//float
C<float> cf;
//double
C<double> cd;
//...
现在看出区别了吧,区别在于函数模板和类模板的使用方式上:
函数
模板在使用时不需要程序员指定类型,编译器在编译的时候会自动绑定;而
类模板需要程序员手工指定,指定的方式是:类名字<类型>。