- 模板类:template <typename T> 说白了就是向之后的内容传递参数类型,把T当作一个数据类型传递,而在声明一个变量的时候,通过base <xxxx> pp; xxx就是传入后面类的数据类型。
// 1.普通template类
template <typename T1> class base
{
public:
T1 a;
};
// 普通类
class base00
{
public:
int e;
};
// 2.Base和Derived类都为template类:derive类可不用typename T
template <typename T>
class derive:public base<T> // 当base作为一个模板类,需要向base传入参数类型,写法就是base<T>,而这个T又是由derive类传入的T作为参数
{
public:
T b;
};
// 3.Base类为特定的template类
class derive2:public base<int> // 当base作为一个模板类,向base传入类型int
{
public:
int c;
};
// 4.Derived类为template类
template <typename T>
class derive3 :public base00
{
public:
T d;
};
int main() {
derive<float> pp;
pp.a = 10.f;
pp.b = 100.0f;
std::cout << "===" << pp.a << "==="<< pp.b<< std::endl;
derive2 tmp;
tmp.c = 99;
tmp.a = 999;
std::cout << "===" << tmp.a << "===" << tmp.c << std::endl;
derive3<int> ttt;
ttt.d = 20;
ttt.e = 200;
std::cout << "===" << ttt.d << "===" << ttt.e << std::endl;
system("pause");
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)