typeid() operator返回type_info,返回值不可拷贝、不可赋值

// Illustrates the typeid operator.
#include <iostream>
#include <typeinfo>
using namespace std; struct PolyBase { virtual ~PolyBase() {} };
struct PolyDer : PolyBase { PolyDer() {} }; struct NonPolyBase {};
struct NonPolyDer : NonPolyBase { NonPolyDer(int) {} }; int main() {
// Test polymorphic Types
const PolyDer pd;
const PolyBase* ppb = &pd;
cout << typeid(ppb).name() << endl;
cout << typeid(*ppb).name() << endl;
cout << boolalpha << (typeid(*ppb) == typeid(pd))
<< endl;
cout << (typeid(PolyDer) == typeid(const PolyDer))
<< endl; // Test non-polymorphic Types
const NonPolyDer npd(1);
const NonPolyBase* nppb = &npd;
cout << typeid(nppb).name() << endl;
cout << typeid(*nppb).name() << endl;
cout << (typeid(*nppb) == typeid(npd)) << endl; // Test a built-in type
int i;
cout << typeid(i).name() << endl; return 0; } ///:~

输出结果是:

struct PolyBase const *
struct PolyDer
true
true
struct NonPolyBase const *
struct NonPolyBase
false
int

The first output line just echoes the static type of ppb because it is a pointer. To get RTTI to kick in, you need to look at the pointer or reference destination object, which is illustrated in the second line. Notice that RTTI ignores top-level const and
volatile qualifiers. With non-polymorphic types, you just get the static type (the type of the pointer itself). As you can see, built-in types are also supported.

内容源自:《TICPP-2nd-ed-Vol-two》

最新文章

  1. HTML中Meta属性http-equiv=&quot;X-UA-Compatible&quot;详解
  2. linux 下使rdate命令支持ipv6 ntp server 同步时间
  3. EL表达式运算符
  4. 使用qt制作简单的加法,乘法运算。
  5. android的liveview装载数据
  6. Python中http请求方法库汇总
  7. C# 2 运算符 if
  8. Sigmoid函数
  9. (一)surging 微服务框架使用系列之surging 的准备工作rabbitmq安装
  10. [AH/HNOI2017]大佬
  11. [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum
  12. [MySQL]select和where子句优化
  13. 关于Xocd升级 cocopoads无法使用的解决
  14. CookieUitl
  15. PMOS 与 NMOS
  16. 理解Sql Server 事务隔离层级(Transaction Isolation Level)
  17. sql server 触发器的简单用法
  18. Linux软硬连接
  19. ASP.NET 动态创建文本框 TextBox (add TextBox to page dynamically)
  20. 自定义控件(视图)2期笔记14:自定义视图之View事件分发 dispatchTouchEvent,onTouch,onTouchEvent,onClick逻辑顺序过程

热门文章

  1. 倒计时Text显示控制
  2. Murano Weekly Meeting 2015.10.20
  3. 设置webstorm的file watch 监视scss文件
  4. Quartz使用(5) - Quartz的Job存储及集群部署
  5. Datastructure
  6. .NET面试题5
  7. Fsharp 类中的空字段
  8. Vue.js基础语法(二)组件
  9. Django Rest Framework进阶二
  10. CSS Grid 布局学习笔记