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. public class 与 class 的区别

    public class 与 class 的区别 1.一个类前面的public是可有可无的 2.如果一个类使用 public 修饰,则文件名必须与类名一致 3.如果一个类前面没有使用public修饰, ...

  2. java && C# 线程

      1.多个线程用到同一个资源的话,必须lock 2.为了解决,在竞争的情况下,优先分配资源给A.就是A和B线程都同时在同一时刻需要资源x,然后的话也不清楚系统是具体怎样调度的.或者说怎样调度,都有可 ...

  3. inventor安装失败怎样卸载安装inventor 2019?

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  4. Keepalived & Lvs集群搭建实验

    实验拓扑图: 实验原理: Keepalived 是基于 LVS ,并与 LVS 高度融合的 LVS和keepalived的关系:lvs起的是负载均衡功能,而keepalived则是高可用(热 备)的支 ...

  5. (转)mysql5.6.7多实例安装、配置的详细讲解分析及shell启动脚本的编写

    一.mysql安装 1.下载mysql数据库源码包: wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.27.tar.gz 2.安装mys ...

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

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

  7. Java入门之JDK安装及环境变量配置

    一.安装JDK 安装过程中会出现两次安装提示 .第一次是安装 jdk ,第二次是安装 jre .建议两个都安装在同一个java文件夹中的不同文件夹中. (注:若无安装目录要求,可全默认设置.无需做任何 ...

  8. Chrome谷歌浏览器中js代码Array.sort排序的bug乱序解决办法

    [现象] 代码如下: var list = [{ n: "a", v: 1 }, { n: "b", v: 1 }, { n: "c", v ...

  9. 随机练习:C#实现维吉尼亚加密与解密(解密前提为已知密匙)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. 常规项目用到的jar包之maven的pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...