c++ type_info and typeid
c++ type_info and typeid
typeid
关键字typeid提供了对一个对象查询类型的功能。 该关键字和dynami_cast一起提供了c++的RTTI(rumtime type identification)支持.
struct MyStruct
{
int i;
};
int _tmain(int argc, _TCHAR* argv[])
{
int i(0);
double j(1.0);
MyStruct myObj;
cout << typeid(int).name() << endl;
cout << typeid(i).name() << endl;
cout << typeid(&i).name() << endl;
cout << typeid(MyStruct).name() << endl;
cout << typeid(myObj).name() << endl;
cout << typeid(&myObj).name() << endl;
return 0;
}
结果为:
但是等等, typeid不是运行时确定的吗, 为什么cout << typeid(int).name() << endl;这样的代码是可以的。 那是因为:
When applied to an expression of polymorphic type, evaluation of a typeid expression may involve runtime overhead (a virtual table lookup), otherwise typeid expression is resolved at compile time.
好吧, c++编译器果然据说是这个世界上最难写的编译器了。
std::type_info
typeid 返回的是类type_info的对象,它由标准库提供,在"typeinfo"中实现。但是我们不能直接使用std::type_info,它提供了以下几个方法:
- operator==
- operator!=
- before
- name
- raw_name
- hash_code
std::type_info info = typeid(int);
type_info::type_info(const type_info &)”: 尝试引用已删除的函数
那是因为type_info的构造函数和operator=函数都被删除的
__CLR_OR_THIS_CALL type_info(const type_info&) = delete;
type_info& __CLR_OR_THIS_CALL operator=(const type_info&) = delete;
但是我们可以这么使用:
const std::type_info& info = typeid(int);
bool same = (info == typeid(int));
cout << same << endl;
总结
理解了 type_info & typeid的用法以及细节
c++ type_info and typeid的更多相关文章
- c++中获得对象类型 typeid 与 type_info
复杂部分略去,摘录要素如下: 1.typeid是C++的关键字之一,等同于sizeof这类的操作符. 2.typeid操作符的返回结果是名为type_info的标准库类型的对象的引用(在头文件type ...
- C++ typeid实现原理
最近看了boost::any类源码,其实现主要依赖typeid操作符.很好奇这样实现的时间和空间开销有多大,决定探一下究竟. VS2008附带的type_info类只有头文件,没有源文件,声明如下: ...
- 从零开始学C++之RTTI、dynamic_cast、typeid、类与类之间的关系uml
一.RTTI Run-time type information (RTTI) is a mechanism that allows the type of an object to be deter ...
- C++中模板单例的跨SO(DLL)问题:RTTI,typeid,static,单例
(转载请注明原创于潘多拉盒子) C++的模板可以帮助我们编写适合不同类型的模板类,给代码的复用性提供了极大的方便.近来写了一个涉及单例的C++模板类,简化下来可以归结为以下的代码: template ...
- RTTI、dynamic_cast、typeid、类与类之间的关系uml
一.RTTI Run-time type information (RTTI) is a mechanism that allows the type of an object to be deter ...
- C++运行时类型判断dynamic_cast和typeid
dynamic_cast dynamic_cast < Type-id > ( expression ) dynamic_cast<类型>(变量) 在运行期间检测类型转换是否安 ...
- 《InsideUE4》UObject(二)类型系统概述
曾子曰:吾日三省吾身--为人谋而不忠乎?与朋友交而不信乎?传不习乎? 引言 上一篇我们谈到了在游戏引擎,或者在程序和高级编程语言中,设计一个统一对象模型得到的好处,和要付出的代价,以及在UE里是怎么对 ...
- C 语言Struct 实现运行类型识别 RTTI
通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型. (2)dynamic_cast: ...
- C++ 关键字浅谈
这里有一个游戏:要求写一个符合C++标准的程序,包含至少十个连续而且不同的关键字.连续是指不能被标识符.运算符.标点符号分割.注意这里的“不同”要求,别想用 int main() { return s ...
随机推荐
- posix and system V IPC
轉載自 http://www1.huachu.com.cn/read/readbook.asp?bookid=10104131 http://www1.huachu.com.cn/read/readb ...
- 树莓派(Raspberry Pi)修改时区
1.安装ntp sudo apt-get install ntpdate 2.修改时区 tzselect 3.选择自己的时区 1)选择大洲 2)选择国家 3)选择城市 4)选择YES 5)或者使用命令 ...
- Mybatis一级、二级缓存
Mybatis一级.二级缓存 一级缓存 首先做一个测试,创建一个mapper配置文件和mapper接口,我这里用了最简单的查询来演示. <mapper namespace="c ...
- iOS中JSONModel的使用
iOS中JSONModel的使用 流弊的JSON数据模型框架 https://github.com/jsonmodel/jsonmodel 版本 1.3.0 如果你喜欢JSONModel,并且使用 ...
- [LintCode] Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Have you met ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- HTML第一节课
html的基本结构<html> <head> <title> 页面标题 </title> </head> <boby> 页面内容 ...
- Golden Gate 概念和机制
1. OGG有哪些进程 ü Manger : manger进程是goldengate的控制进程,分别运行在源端和目标端上,它主要的作用是启动.监控.重启goldengate的其他进程,报告错误及事件 ...
- 面向过程—面向对象(C++的封装,this)_内存四区_变量生命周期
1.面向对象主要涉及 构造函数.析构函数.虚函数.继承.多态等. 2.对各种支持 的底层实现机制 c语言中,数据 和 处理数据的操作(函数) 是分开来声明,即语言本身并没有支持 “数据和函数”的关联 ...
- IOS第18天(10,核心动画-转盘,自定义buton,旋转动画)
*****HMViewController.m #import "HMViewController.h" #import "HMWheelView.h" @in ...