//C++模板类复习
#include
using namespace std;
template
class test
{
private:
T1 temp1;
T2 temp2;
public:
test(){}
test(T1 data1, T2 data2):temp1(data1),temp2(data2){}
void show();
};
template
//定义模板类成员
void test
::show()
{
cout << "原始类" << endl;
cout << temp1 << endl << temp2 << endl;
}
template <> //显示具体化
class test
{
private:
int temp1;
int temp2;
public:
test(){}
test(int one, int two):temp1(one), temp2(two){}
void show()
{
cout << "具体化" << endl;
cout << temp1 << endl << temp2 << endl;
}
};
template
class test
{
private:
T temp1;
double temp2;
public:
test(){}
test(T data1, double data2):temp1(data1), temp2(data2){}
void show()
{
cout << "部分具体化" << endl;
cout << temp1 << endl << temp2 << endl;
}
};
template class test
; //模板类的显示具体化和显示实例化不会冲突,因为显示具体化不会创建类声明
//将模板类作为内置成员
template
class test1
{
private:
template
class test2
{
private:
T2 data2;
public:
test2(){}
test2(T2 d):data2(d){}
void show2()
{
cout << data2 << endl;
}
};
test2
data1;
public:
test1(T1 d):data1(d){}
void show1()
{
data1.show2();
}
};
template
class te>
class test3
{
private:
te
a;
public:
test3(te
d):a(d){}
void show1()
{
a.show();
}
};
int main()
{
//test
a(1, "linukey");//第二个参数将用默认参数string
//a.show();
//test
a(5, 6);//隐式实例化
//a.show();
//test
a(5, 6);//因为有了int,int 的显示具体化,所以此次使用具体化类
//a.show();
//test
a(1, 2.1);//部分具体化
//a.show();
//test1
a(1); //模板类作为类内置成员
//a.show1();
//test
a(1, 2); //模板类作为参数
//test3
b(a);
//b.show1();
return 0;
}
|