1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 class Type{
6 public:
7 string Name;
8 };
9
10 class O{
11 public:
12 virtual void ToString(){
13 cout<< "O" << endl;
14 }
15 virtual Type GetType(){
16 Type t;
17 t.Name="O";
18 return t;
19 }
20 };
21
22 template<class T>
23 class V : public O{
24 public:
25 V<T>(){
26
27 }
28 public:
29 void ToString(){
30 Type t=T::ClassType();
31 cout<< t.Name << endl;
32 }
33 };
34
35
36
37 class D: public V<D>{
38 public:
39 static Type ClassType(){
40 Type t;
41 t.Name="D";
42 return t;
43 }
44 public:
45 D() { }
46
47 Type GetType(){
48 Type t;
49 t.Name="D";
50 return t;
51 }
52 void ToString(){
53 cout << "DDDD" << endl;
54 }
55 };
56
57
58
59 int main(){
60 O* o =new O();
61 o->ToString();
62
63 D* d =new D();
64 d->ToString();
65
66 cin.get();
67 }
1 //下面的代码 耗时完全相同 —— 指针类型的转换 完全不浪费任何时间
2 cout<<"AAA"<<endl;
3 for(long long i=0;i< 1024*1024*1024;i++) //每秒3.5Y次左右
4 {
5 //这两种指针转换 性能完全相同
6 //O* t =(O*)d;
7 O* t = static_cast<O*>(d);
8 }
9 cout<<"BBB"<<endl;
10 for(long long i=0;i< 1024*1024*1024;i++)
11 {
12 }
13 cout<<"CCC"<<endl;