C++ 类模板、函数模板
函数模板
函数模板相当于java中的泛型方法
template <typename T>
T test5(T i, T j){
return i > j ? i : j;
}
void main(){
double f = test5(13.2, 15.3);
cout << f << endl;
}
打印结果为15.3
类模板
类模板相当于java的泛型类
template < class T,class E>
class Q {
public:
T test(T t, E e){
return t + e;
}
};
void main(){
Q<int, float> q;
cout << q.test(1,1.1) << endl;
}
打印结果为2
还没有评论,来说两句吧...