模板类的继承包括四种: 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://blog.csdn.net/foryourface/article/details/21090467
template <class T1> class BaseT { BaseT(); } template <class T1> BaseT<T1>::BaseT() { }; template<class T2> class InheritT : public BaseT<class T2> { InheritT(); } template<class T2> InheritT<T2>::InheritT:BaseT<T2>() { } class CInherit: public BaseT<int> { CInherit(); } CInherit::CInherit:BaseT<int>() { }
http://blog.csdn.net/zblue78/article/details/5563654