- #include <string>
- #include <time.h>
- #include <iostream>
- #include <unordered_map>
- using namespace std;
- namespace
- {
- class A
- {
- public:
- static const char* classType();
- virtual void* queryObject(const char* classType) const;
- };
- const char* A::classType()
- {
- const char* s_classType = "A";
- return s_classType;
- }
- void* A::queryObject(const char* classType) const
- {
- if (classType == A::classType())
- return const_cast<A*>(this);
- return nullptr;
- }
- class B
- {
- public:
- static const char* classType();
- virtual void* queryObject(const char* classType) const;
- };
- const char* B::classType()
- {
- const char* s_classType = "B";
- return s_classType;
- }
- void* B::queryObject(const char* classType) const
- {
- if (classType == B::classType())
- return const_cast<B*>(this);
- return nullptr;
- }
- class C : public A, public B
- {
- public:
- static const char* classType();
- void* queryObject(const char* classType) const override;
- };
- const char* C::classType()
- {
- const char* s_classType = "C";
- return s_classType;
- }
- void* C::queryObject(const char* classType) const
- {
- if (classType == C::classType())
- return const_cast<C*>(this);
- if (void* res = A::queryObject(classType))
- return res;
- if (void* res = B::queryObject(classType))
- return res;
- return nullptr;
- }
- class D : public A
- {
- public:
- static const char* classType();
- void* queryObject(const char* classType) const override;
- };
- const char* D::classType()
- {
- const char* s_classType = "D";
- return s_classType;
- }
- void* D::queryObject(const char* classType) const
- {
- if (classType == D::classType())
- return const_cast<D*>(this);
- return A::queryObject(classType);
- }
- template <typename To, typename From>
- To* fast_dynamic_cast(const From* from)
- {
- if (!from)
- return nullptr;
- return static_cast<To*>(from->queryObject(To::classType()));
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- A* a = new C();
- cout <<"A* a = new C();" <<endl;
- clock_t start, stop;
- const int count = 100000000;
- cout << "===== cast from pointer A to pointer C, should be not null =====" << endl;
- C* c = nullptr;
- start = clock();
- for (int i = 0; i < count; i++)
- {
- c = dynamic_cast<C*>(a);
- }
- stop = clock();
- cout <<"dynamic_cast from A to C:" <<c << "," << "Time:" << stop - start << endl;
- c = nullptr;
- start = clock();
- for (int i = 0; i < count; i++)
- {
- c = static_cast<C*>(a);
- }
- stop = clock();
- cout <<"static_cast from A to C:" <<c << "," << "Time:" << stop - start << endl;
- c = nullptr;
- start = clock();
- for (int i = 0; i < count; i++)
- {
- c = fast_dynamic_cast<C>(a);
- }
- stop = clock();
- cout <<"fast_dynamic_cast from A to C:" <<c << "," << "Time:" << stop - start << endl;
- cout << "\n===== cast from pointer A to pointer B, should be not null =====" << endl;
- B* b = nullptr;
- start = clock();
- for (int i = 0; i < count; i++)
- {
- b = dynamic_cast<B*>(a);
- }
- stop = clock();
- cout <<"dynamic_cast from A to B:" <<b << "," << "Time:" << stop - start << endl;
- b = nullptr;
- start = clock();
- for (int i = 0; i < count; i++)
- {
- //b = static_cast<B*>(a); //compiler error
- }
- stop = clock();
- cout <<"static_cast from A to B:" <<"compiler error" << endl;
- b = nullptr;
- start = clock();
- for (int i = 0; i < count; i++)
- {
- b = fast_dynamic_cast<B>(a);
- }
- stop = clock();
- cout <<"fast_dynamic_cast from A to B:" <<b << "," << "Time:" << stop - start << endl;
- cout << "\n===== cast from pointer A to pointer D, should be null =====" << endl;
- D* d = nullptr;
- start = clock();
- for (int i = 0; i < count; i++)
- {
- d = dynamic_cast<D*>(a);
- }
- stop = clock();
- cout <<"dynamic_cast from A to D:" <<d << "," << "Time:" << stop - start << endl;
- d = nullptr;
- start = clock();
- for (int i = 0; i < count; i++)
- {
- d = static_cast<D*>(a);
- }
- stop = clock();
- cout <<"static_cast from A to D:" <<d << "," << "Time:" << stop - start << endl;
- d = nullptr;
- start = clock();
- for (int i = 0; i < count; i++)
- {
- d = fast_dynamic_cast<D>(a);
- }
- stop = clock();
- cout << "fast_dynamic_cast from A to D:" << d << "," << "Time:" << stop - start << endl;
- delete a;
- a = nullptr;
- return 0;
- }
来源: https://www.cnblogs.com/ldlchina/p/8698346.html