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. caffe学习资料

    1.利用Caffe做回归(regression) http://www.cnblogs.com/frombeijingwithlove/p/5314042.html

  2. pat1031. Hello World for U (20)

    1031. Hello World for U (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Giv ...

  3. 【Python学习一】使用Python+selenium实现第一个自动化测试脚本

    1.Python的下载 python官方下载地址:https://www.python.org/downloads/ 这边安装的3.6.5为最新版本以适应未来的需求 进入页面就有两个版本的下载选择,2 ...

  4. Day7下

    T1 我直接就用的LCA ,可能慢点.反正数据试过了. T2 期望dp不会啊. T3 好麻烦.

  5. 利用DOS命令做伪装成图片的压缩包,看上去是图片其实是个压缩包用2条命令即搞定

    在很多地方我们看到一张图片,然后把这张图片下载到本地,改后缀名为xx.rar,即变成了压缩包. 比如下面这个图片:(把以下图片保存到本地,后缀名xx.png 改为 xx.rar,解压试试) 怎么样,是 ...

  6. $.ajax、$.post[转]

    jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求 参数: url (String) : 发送请求的URL地址. data ...

  7. webpack-webpackConfig-plugin 配置

    ProvidePlugin 语法: module.export = { plugins: [ new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jqu ...

  8. Java—常量和变量

    关键字 Java中有特殊用途的词被称为关键字,关键字服务大小写. 标识符 标识符是用于给java程序中的变量.类.方法等命名的符号. 标识符的几条规则: 由字母.数字.下划线(_).美元符号($)组成 ...

  9. GitLab 的安装及汉化

    系统环境:CentOS7 切记:安装GitLab 时会自动安装GitLab自带的Nginx,为了避免冲突,部署环境时先不要安装Nginx. 官网安装及汉化安装 官网安装最新版GitLab:https: ...

  10. MySQL入门很简单: 11 mysql函数

    1. 数学函数 2. 字符串函数 3. 日期和时间函数 4. 条件判断函数 IF(expr, v1, v2) // 如果表达式expr成立,返回结果v1,否则返回v2: IFNULL(v1, v2) ...