cpp中typeid
来源:互联网 发布:自己没货如何开淘宝店 编辑:程序博客网 时间:2024/05/01 10:37
cpp中typeid举例:
#include <iostream>#include <typeinfo> //for 'typeid'using namespace std;class Person {public: // ... Person members ... virtual ~Person() {}};class Employee : public Person { // ... Employee members ...};int main(){ Person person; Employee employee; Person *ptr = &employee; Person &ref = employee; // The string returned by typeid::name is implementation-defined std::cout << typeid(person).name() << std::endl; // Person (statically known at compile-time) std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time) std::cout << typeid(ptr).name() << std::endl; // Person * (statically known at compile-time) std::cout << typeid(*ptr).name() << std::endl; // Employee (looked up dynamically at run-time // because it is the dereference of a // pointer to a polymorphic class) std::cout << typeid(ref).name() << std::endl; // Employee (references can also be polymorphic) Person* p = 0; try { typeid(*p); // not undefined behavior; throws std::bad_typeid // *p, *(p), *((p)), etc. all behave identically }catch (...){ cout << "NULL pointer!"<<endl; } Person& pRef = *p; // undefined behavior, dereferences null typeid(pRef); // does not meet requirements to throw std::bad_typeid Employee e; Person pp; Person &per_ref = pp; try{ Employee & em_ref = dynamic_cast<Employee&>(per_ref); }catch(bad_cast e){ cout << "bad cast : not valid type reference !" <<endl; }catch(...){ cout << "not valid type reference !" <<endl; } // because the expression for typeid is not the result // of applying the unary * operator; behavior is undefined}
- cpp中typeid
- TypeID.cpp
- C++中typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- C++中获取数据类型typeid
- C++中typeid的用法
- G-Rilling EMD工具箱
- 【实习生笔试面试】腾讯2013实习生电话面试总结
- eclipse中MAVEN的web项目部署至TOMCAT的步骤
- pyqt4 使用qrc 文件
- Android 的系统属性(SystemProperties)设置分析
- cpp中typeid
- Java中使用LUA脚本语言
- wince下实现拷屏保存为bmp,装载jpg格式图片转成bmp格式图片
- XML基本简介
- AT指令中文手册
- Hyperion 数据源简介
- 说说Android瀑布流的内存管理
- UIImageView 实现水平翻转
- linux 信号处理函数一个例子