昨天刚买了本书——《C++ STL 技术开发导引》。挺贵的,五十几。不过为了学习,也就不那么在乎了。刚才做了几个模板类的东西。现在还觉得晕晕的,不过比起以前来倒是清楚许多了。哎,程序这东西有时候真的让人摸不着头脑。
#include<iostream> #include<vector> #include<algorithm> #include<iterator> using namespace std; template<class T> class A { public: void func(T a) { cout<<"aaaaaa"<<endl; } }; template<class T1,class T2> class B: public A<T1> { T1 i; public: B(T1 m){m = i;} void func(T1 t1, T2 t2) { cout<<"daafasd"<<endl; } void inc(T1 t1,T2 t2); }; template<class T1,class T2> void B<T1,T2>::inc(T1 t1,T2 t2) { cout<<t1 + t2<<endl; } void main() { A<int> a; a.func(10); B<double,double>b(10); b.func(10,60); b.inc(10,20); }
上面的程序既有模板类,也有模板函数,也有模板类的继承。要注意什么时候应该写template<class T>这样的语句。模板的特化我就暂时不看了,怕混淆。其实也不难。