『C++』Temp_2018_12_06

时间:2023-03-09 18:11:05
『C++』Temp_2018_12_06
 #include <iostream>
#include <string>
using namespace std; class Type{
public:
string Name;
}; class O{
public:
virtual void ToString(){
cout<< "O" << endl;
}
virtual Type GetType(){
Type t;
t.Name="O";
return t;
}
}; template<class T>
class V : public O{
public:
V<T>(){ }
public:
void ToString(){
Type t=T::ClassType();
cout<< t.Name << endl;
}
}; class D: public V<D>{
public:
static Type ClassType(){
Type t;
t.Name="D";
return t;
}
public:
D() { } Type GetType(){
Type t;
t.Name="D";
return t;
}
void ToString(){
cout << "DDDD" << endl;
}
}; int main(){
O* o =new O();
o->ToString(); D* d =new D();
d->ToString(); cin.get();
}
     //下面的代码 耗时完全相同 —— 指针类型的转换 完全不浪费任何时间
cout<<"AAA"<<endl;
for(long long i=;i< **;i++) //每秒3.5Y次左右
{
//这两种指针转换 性能完全相同
//O* t =(O*)d;
O* t = static_cast<O*>(d);
}
cout<<"BBB"<<endl;
for(long long i=;i< **;i++)
{
}
cout<<"CCC"<<endl;