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