(1)ATL如何使用模板类
1 #include <iostream>
2 using namespace std;
3
4 class CBase
5 {
6 public:
7 CBase(){}
8 ~CBase(){}
9
10 void BaseMethod()
11 {
12 cout << "BaseMethod in Base" << endl;
13 }
14 };
15
16 class CDerived : public CBase
17 {
18 public:
19 CDerived() {}
20 ~CDerived() {}
21 };
22
23 template<class T>
24 class CComObject : public T
25 {
26 public:
27 CComObject() {}
28 ~CComObject() {}
29
30 void CallBaseMethod()
31 {
32 T * pT = static_cast<T*>(this);
33 pT->BaseMethod();
34 }
35 };
36
37 int main()
38 {
39 CComObject<CDerived> * pDerived = new CComObject<CDerived>;
40 pDerived->CallBaseMethod();
41 delete pDerived;
42 }
为了更加快速和高效,ATL尽量避免使用虚函数,因为在大的类层次中,调用虚函数的代价比较高(运行速度和代码增长)
时间: 2024-10-10 12:28:14