注意以下代码:
template<class MYT>
class NEWCLASS: public MYT
这种用法是我最近才发现的,在WTL中,如果我们用类向导创建的单文档中,有一个CAboutDlg这个类中就用到的了这种方式。
哪位大神,帮我瞧瞧这种方法的优缺点在哪里?在什么场合下使用比较多?
#include <iostream> using namespace std; class A { public: void SetData(int _First = 0, int _Second = 0 ) { a = _First; b = _Second; } void Show() { cout<<"A::a: "<<a<<endl; cout<<"A::b: "<<b<<endl; } private: int a; int b; }; template<class MYT> class NEWCLASS: public MYT { public: void SetNewClassData(int _data = 5) { c = _data; } void NewClassShow() { cout<<"NEWCLASS::c: "<<c<<endl; } private: int c; }; int main() { typedef NEWCLASS<A> STDClass; STDClass a; a.SetData(5,6); a.SetNewClassData(8); a.Show(); a.NewClassShow(); return 0; }
另外一个例子,代码是没有任何问题的,我更想知道的是这些大概有什么用处?
#include <iostream> using namespace std; class A { public: void SetData(int _First = 0, int _Second = 0 ) { a = _First; b = _Second; } void Show() { cout<<"A::a: "<<a<<endl; cout<<"A::b: "<<b<<endl; } private: int a; int b; }; class B { public: void BSetData(int _First = 0) { d = _First; } void BShow() { cout<<"B::d: "<<d<<endl; } private: int d; }; template<class MYT> class NEWCLASS: public MYT,public B { public: void SetNewClassData(int _data = 5) { c = _data; } void NewClassShow() { cout<<"NEWCLASS::c: "<<c<<endl; } private: int c; }; int main() { typedef NEWCLASS<A> STDClass; STDClass a; a.SetData(5,6); a.SetNewClassData(8); a.BSetData(66); a.Show(); a.BShow(); a.NewClassShow(); return 0; }