100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > C++ 模板类的继承

C++ 模板类的继承

时间:2022-04-20 02:55:49

相关推荐

C++ 模板类的继承

模板类: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 Ttemplate <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;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。