C++中的RTTI机制
2014-03-05 14:19
197 查看
C++中的RTTI机制
2014年1月21日作者:果冻想70 views暂无评论
前言
RTTI是”Runtime Type Information”的缩写,意思是运行时类型信息,它提供了运行时确定对象类型的方法。RTTI并不是什么新的东西,很早就有了这个技术,但是,在实际应用中使用的比较少而已。而我这里就是对RTTI进行总结,今天我没有用到,并不代表这个东西没用。学无止境,先从typeid函数开始讲起。
typeid函数
typeid的主要作用就是让用户知道当前的变量是什么类型的,比如以下代码:果冻想 | 一个原创文章分享网站
C++
123456789101112131415161718192021222324 | #include <iostream>#include <typeinfo>using namespace std; int main(){ short s = 2; unsigned ui = 10; int i = 10; char ch = 'a'; wchar_t wch = L'b'; float f = 1.0f; double d = 2; cout<<typeid(s).name()<<endl; // short cout<<typeid(ui).name()<<endl; // unsigned int cout<<typeid(i).name()<<endl; // int cout<<typeid(ch).name()<<endl; // char cout<<typeid(wch).name()<<endl; // wchar_t cout<<typeid(f).name()<<endl; // float cout<<typeid(d).name()<<endl; // double return 0;} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <iostream> #include <typeinfo> using namespace std; class A { public: void Print() { cout<<"This is class A."<<endl; } }; class B : public A { public: void Print() { cout<<"This is class B."<<endl; } }; struct C { void Print() { cout<<"This is struct C."<<endl; } }; int main() { A *pA1 = new A(); A a2; cout<<typeid(pA1).name()<<endl; // class A * cout<<typeid(a2).name()<<endl; // class A B *pB1 = new B(); cout<<typeid(pB1).name()<<endl; // class B * C *pC1 = new C(); C c2; cout<<typeid(pC1).name()<<endl; // struct A * cout<<typeid(c2).name()<<endl; // struct A return 0; } |
type_info类
去掉那些该死的宏,在Visual Studio 2012中查看type_info类的定义如下:果冻想 | 一个原创文章分享网站
C++
12345678910111213141516171819 | class type_info{public: virtual ~type_info(); bool operator==(const type_info& _Rhs) const; // 用于比较两个对象的类型是否相等 bool operator!=(const type_info& _Rhs) const; // 用于比较两个对象的类型是否不相等 bool before(const type_info& _Rhs) const; // 返回对象的类型名字,这个函数用的很多 const char* name(__type_info_node* __ptype_info_node = &__type_info_root_node) const; const char* raw_name() const;private: void *_M_data; char _M_d_name[1]; type_info(const type_info& _Rhs); type_info& operator=(const type_info& _Rhs); static const char * _Name_base(const type_info *,__type_info_node* __ptype_info_node); static void _Type_info_dtor(type_info *);}; |
typeid函数的使用
typeid使用起来是非常简单的,常用的方式有以下两种:使用type_info类中的name()函数返回对象的类型名称就像上面的代码中使用的那样;但是,这里有一点需要注意,比如有以下代码:
果冻想 | 一个原创文章分享网站C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> #include <typeinfo> using namespace std; class A { public: void Print() { cout<<"This is class A."<<endl; } }; class B : public A { public: void Print() { cout<<"This is class B."<<endl; } }; int main() { A *pA = new B(); cout<<typeid(pA).name()<<endl; // class A * cout<<typeid(*pA).name()<<endl; // class A return 0; } |
果冻想 | 一个原创文章分享网站
C++
1234567891011121314151617181920212223 | #include <iostream>#include <typeinfo>using namespace std; class A{public: virtual void Print() { cout<<"This is class A."<<endl; }}; class B : public A{public: void Print() { cout<<"This is class B."<<endl; }}; int main(){ A *pA = new B(); cout<<typeid(pA).name()<<endl; // class A * cout<<typeid(*pA).name()<<endl; // class B return 0;} |
使用type_info类中重载的==和!=比较两个对象的类型是否相等
这个会经常用到,通常用于比较两个带有虚函数的类的对象是否相等,例如以下代码:
果冻想 | 一个原创文章分享网站C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <iostream> #include <typeinfo> using namespace std; class A { public: virtual void Print() { cout<<"This is class A."<<endl; } }; class B : public A { public: void Print() { cout<<"This is class B."<<endl; } }; class C : public A { public: void Print() { cout<<"This is class C."<<endl; } }; void Handle(A *a) { if (typeid(*a) == typeid(A)) { cout<<"I am a A truly."<<endl; } else if (typeid(*a) == typeid(B)) { cout<<"I am a B truly."<<endl; } else if (typeid(*a) == typeid(C)) { cout<<"I am a C truly."<<endl; } else { cout<<"I am alone."<<endl; } } int main() { A *pA = new B(); Handle(pA); delete pA; pA = new C(); Handle(pA); return 0; } |
dynamic_cast的内幕
在这篇《static_cast、dynamic_cast、const_cast和reinterpret_cast总结》的文章中,也介绍了dynamic_cast的使用,对于dynamic_cast到底是如何实现的,并没有进行说明,而这里就要对于dynamic_cast的内幕一探究竟。首先来看一段代码:果冻想 | 一个原创文章分享网站
C++
123456789101112131415161718192021222324252627282930313233 | #include <iostream>#include <typeinfo>using namespace std; class A{public: virtual void Print() { cout<<"This is class A."<<endl; }}; class B{public: virtual void Print() { cout<<"This is class B."<<endl; }}; class C : public A, public B{public: void Print() { cout<<"This is class C."<<endl; }}; int main(){ A *pA = new C; //C *pC = pA; // Wrong C *pC = dynamic_cast<C *>(pA); if (pC != NULL) { pC->Print(); } delete pA;} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <iostream> #include <typeinfo> using namespace std; class A { public: virtual void Print() { cout<<"This is class A."<<endl; } }; class B : public A { public: void Print() { cout<<"This is class B."<<endl; } }; class C : public A { public: void Print() { cout<<"This is class C."<<endl; } }; void Handle(A *a) { if (dynamic_cast<B*>(a)) { cout<<"I am a B truly."<<endl; } else if (dynamic_cast<C*>(a)) { cout<<"I am a C truly."<<endl; } else { cout<<"I am alone."<<endl; } } int main() { A *pA = new B(); Handle(pA); delete pA; pA = new C(); Handle(pA); return 0; } |
总结
我在这里总结了RTTI的相关知识,希望大家看懂了。这篇博文有点长,希望大家也耐心的看。总结了就会有收获。2014年1月21日 于大连,东软。
相关文章推荐
- [置顶] C++中RTTI机制剖析
- C++中的RTTI机制解析
- C++RTTI和反射机制
- C++ 模板 decltype关键字 RTTI机制
- 如何打开C++的RTTI机制
- C++中的RTTI机制详解
- 对于C++的RTTI机制的初步思考
- 《高质量程序设计指南——C/C++》第15章 C++异常处理机制和RTTI
- 对于C++的RTTI机制的初步思考
- C++中的RTTI机制解析
- [置顶] C++中RTTI机制剖析
- C++中的RTTI机制详解
- C++中的RTTI(runtime type information) 机制
- C++中的RTTI机制解析
- c++的RTTI机制详解
- C++中RTTI机制剖析
- C++中的RTTI机制详解
- Delphi 的RTTI机制浅探二
- 混迹于C++之继承机制中的构造器和析构器
- C++虚函数表机制解析(转)