标签:
会用继承,会用一些模板,
但是模板的继承是什么鬼;
百度如下:
模板类的继承包括四种:
1.(普通类继承模板类)
template<class T> class TBase{ T data; …… }; class Derived:public TBase<int>{ …… };
2.(模板类继承了普通类(非常常见))
class TBase{ …… }; template<class T> class TDerived:public TBase{ T data; …… };
3.(类模板继承类模板)
template<class T> class TBase{ T data1; …… }; template<class T1,class T2> class TDerived:public TBase<T1>{ T2 data2; …… };
4.(模板类继承类模板,即继承模板参数给出的基类)
#include<iostream> using namespace std; class BaseA{ public: BaseA(){cout<<"BaseA founed"<<endl;} }; class BaseB{ public: BaseB(){cout<<"BaseB founed"<<endl;} }; template<typename T, int rows> class BaseC{ private: T data; public: BaseC():data(rows){ cout<<"BaseC founed "<< data << endl;} }; template<class T> class Derived:public T{ public: Derived():T(){cout<<"Derived founed"<<endl;} }; void main() { Derived<BaseA> x;// BaseA作为基类 Derived<BaseB> y;// BaseB作为基类 Derived<BaseC<int, 3> > z; // BaseC<int,3>作为基类 }
标签:
原文地址:http://www.cnblogs.com/bmyell1234/p/5002823.html