利用显示类说明可以扩展通用类的用途。
测试代码:
#include<iostream>
using namespace std;
//通用类
template <class T> class myclass{
T x;
public:
myclass(T a)
{
x=a;
cout<<"This is in generic myclass"<<endl;
}
};
//显示类说明
template <> class myclass<int>{
int x;
public:
myclass(int a)
{
cout<<"This is in specialization/n";
x=a*a;
}
};
int main()
{
myclass<double> class1(1.0);
myclass<int> class2(2);
return 0;
}
测试结果:
This is in generic myclass
This is in specialization
请按任意键继续. . .