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 ...
随机推荐
- 百度ueditor 拖文件或world 里面复制粘贴图片到编辑中 上传到第三方问题
我这边从world 里面复制粘贴图片到编辑器中,它自动给我上传了,但是我是用的第三方的要设置一个token值,我找了很久,也没有找到应该在哪里设置这个上传的参数,如果是点击图片上传,我知道在dialo ...
- [LintCode] Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Have you met ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- Ubuntu FTP 配置
1. apt-get install vsftpd 2. vim /etc/vsftp.conf #禁止匿名访问 anonymous_enable=NO #接受本地用户 local_enable=YE ...
- Daily Scrum 11.3
今天开发人员的任务都还行,测试的同学要开始辛苦了. 下面是今天的Task统计:
- JAVA 往jar包添加class文件
(1) jar -uf jarfile.jar yourclasses (2) 右击要打包的文件夹,选择“添加到压缩文件”,弹出对话框: 把压缩文件格式改为zip,再把压缩文件名中的反缀改为.jar, ...
- Console ArcEngine 许可绑定
using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Geodatabase;using ESRI.ArcGIS.DataSourcesFile; using ESRI. ...
- 对ASM存储管理的一些初步理解记录
ASM:Automatic Storage Management,是ORACEL10G以后为了简化存储管理的复杂性,也是为了摆脱对其他厂商的依赖而推出的.ASM作为目前ORACLE推荐的首选存储方案, ...
- 安装好php后,配置httpd以便支持php3脚本
Apache是目前应用最广的Web服务器,PHP是一种类似ASP的易学的脚本语言,而且性能和功能都比ASP要强,而MySQL又是一个Linux上应用最多的数据库系统,特别是用于网站建设,这3个软件均是 ...
- 创建一个LinkedList,然后在其中插入多个值,确保每个值都插入到List中间(偶数中间两个数之一,奇数在正中间)
这是Thinking in java 中的一道题,下面是我的解决方案: package test; import java.util.LinkedList; import java.util.List ...