C++中的RTTI(runtime type information) 机制
来源:互联网 发布:淘宝直通车关键词价格 编辑:程序博客网 时间:2024/05/29 07:39
#include <iostream>#include <typeinfo>using namespace std;class Shape {public:virtual void Print()=0;};class Circle:public Shape {public:virtual void Print() override {cout << "this is a Circle" << endl;}};class Square:public Shape {public:virtual void Print() override {cout << "this is a Square" << endl;}};int main() {Shape *s[2];s[0] = new Circle;s[1] = new Square;for(int i=0;i<2; i++) {//1.运行时转型,必须含有虚函数virtual,安全向下转型//RTTI(runtime type information)if(dynamic_cast<Circle*>(s[i])) {cout << "hello Circle" << endl;s[i]->Print();}if(dynamic_cast<Square*>(s[i])) {cout << "hello Square" << endl;s[i]->Print();}}cout << typeid(*s[0]).name() << endl; //typeid是运行时期的事情,也就是动态类型cout << typeid(Circle).name() << endl;if(typeid(Circle).name() == typeid(*s[0]).name()) {cout << "p is point to a Circle object" << endl;((Circle*)s[0])->Print();}//type_info ti = typeid(Circle); //privatereturn 0;}/*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 *);};*/
阅读全文
1 0
- C++中的RTTI(runtime type information) 机制
- C++ typeid 与RTTI(Runtime Type Information)
- RTTI(runtime type information Identification) 运行时类型识别
- C++ - RTTI(RunTime Type Information)运行时类型信息 详解
- think in java笔记:RTTI-Runtime type information
- RTTI (Runtime Type Identification)
- RTTI(Runtime Type Identification)
- 关于runtime type information
- 三十三、RTTI(runtime type information)、dynamic_cast运算符、typeid运算符、type_info
- Objective-C中的Runtime机制
- MFC中的RTTI(Runtime Type Identification, 运行时类型识别)详解(参考《深入浅出MFC》)
- BCB中的RTTI机制
- BCB中的RTTI机制
- C++中的RTTI机制
- C++中的RTTI机制
- <C++> RTTI机制
- Thinking in Java笔记:Runtime Type Information
- C++虚函数(8) - RTTI(Run-time type information)
- 指针数组和数组指针
- c++ type trait 之 类型判断工具
- 企业人事管理系统
- 数据结构——图的遍历和存储
- Jquery用法
- C++中的RTTI(runtime type information) 机制
- Matlab自适应滤波器设计Demo——LMS,RLS
- 04-树4 是否同一棵二叉搜索树 (25分)
- windows 下 Redis 主从 读写分离
- Entity Framework- Databinding with WinForms
- 应付考试用的KMP算法中next数组及nextval数组的计算(笑)
- 北大 AI 公开课第8讲:华为李航——NLP 有 5 个基本问题,深度学习有4个做得很好 (PPT)
- 5-28 猴子选大王 (20分)
- AOP学习之路