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》

typeid操作符的更多相关文章

  1. 4.8 C++ typeid操作符

    参考:http://www.weixueyuan.net/view/6378.html 总结: typeid操作符用于判断表达式的类型,注意它和sizeof一样是一个操作符而不是函数. 如果需要使用t ...

  2. typeid详解(转)

    (http://www.cppblog.com/smagle/archive/2010/05/14/115286.html) 在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time ...

  3. c++ typeid获取类型名-rtti

    typeid操作符的作用就是获取一个表达式的类型.返回结果是const type_info&.不同编译器实现的type_info class各不相同.但c++标准保证它会实现一个name()方 ...

  4. typeid详解

    在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time Type Identification,运行时类型识别),它使程序能够获取由基指针或引用所指向的对象的实际派生类型,即允许“ ...

  5. 使用typeid(变量或类型).name()来获取常量或变量的类型---gyy整理

    使用typeid(变量或类型).name()来获取常量或变量的类型 <typeinfo>  该头文件包含运行时类型识别(在执行时确定数据类型)的类 typeid的使用   typeid操作 ...

  6. C++ typeid实现原理

    最近看了boost::any类源码,其实现主要依赖typeid操作符.很好奇这样实现的时间和空间开销有多大,决定探一下究竟. VS2008附带的type_info类只有头文件,没有源文件,声明如下: ...

  7. 如何在C++中获得完整的类型名称(RTTI的typeid在不同平台下有不同的输出值表达,自建类改进了RTTI丢失的信息)

    Wrote by mutouyun. (http://darkc.at/cxx-get-the-name-of-the-given-type/)   地球人都知道C++里有一个typeid操作符可以用 ...

  8. 从零开始学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 ...

  9. c++ 动态判断基类指针指向的子类类型(typeid)

    我们在程序中定义了一个基类,该基类有n个子类,为了方便,我们经常定义一个基类的指针数组,数组中的每一项指向都指向一个子类,那么在程序中我们如何判断这些基类指针是指向哪个子类呢? 本文提供了两种方法 ( ...

随机推荐

  1. Unity String 转换成 Vector3

  2. 性能测试工具LoadRunner28-LR之内部数据参数类型

    Date/Time 在“Parameter type”中您可以选择Date/Time,即:用当前的日期/时间替换参数.要指定日期/时间的格式,可以从格式列表中选择一个格式,或者指定您自己的格式. [l ...

  3. If you are tired...

    如果你累了 1. 深呼吸 放松身体,深呼吸五分钟. 2. 听音乐 静静地听几首歌放松一下就好了,比如王豪学长推荐的追梦赤子心,骄傲的少年. 3. 冥想 放松身体,处于冥想状态. 4. 干洗脸.鸣天鼓. ...

  4. nexus 私服相关的配置

    1 上传到nexus的配置 1 settings.xml <server> <id>releases</id> <username>admin</ ...

  5. JavaScript Date 学习心得

    1.要创建一个日期对象,使用new 操作符和Date构造函数即可: var date=new Date() 在调用Date构造函数而不传递参数的情况下,新创建的对象可以自动获得当前日期和时间.必须传入 ...

  6. 文本框只允许输入数字.net/javascript

    <input type="text" name="test" onKeyUp="test1.value=(this.value=this.val ...

  7. java解析json串常识

    注意:JSONObject 和JSONArray的使用区别 报错:A JSONObject text must begin with '{' at character 1 of 分析:  JSONOb ...

  8. 卸载jrebel

    Setting-Plugins-搜索Jrebel-右击选择Uninstall-apply 确认重启idea即可

  9. Linux系统如何设置开机程序自启动

    在Linux系统如何让程序开机时自动启动      核心提示:系统的服务在开机时一般都可以自动启动,那在linux系统下如果想要程序在开机时自动启动怎么办?我们知道在 windows系统“开始”--& ...

  10. ARM实验3 ——串口实验

    uart串口实验 实验内容: 编写UART模块程序,通过串口将信息打印到终端. 实验目的: 熟悉开发环境的使用. 掌握exynos4412处理器的UART功能. 实验平台: FS4412开发板,ecl ...