In computer programming, run-time type information or run-time type identification (RTTI)[1] refers to a C++ mechanism that exposes information about an object's data type at runtime. Run-time type information can apply to simple data types, such as integers and characters, or to generic types. This is a C++ specialization of a more general concept called type introspection. Similar mechanisms are also known in other programming languages, such as Object Pascal (Delphi).

In the original C++ design, Bjarne Stroustrup did not include run-time type information, because he thought this mechanism was often misused.[2]

typeid[edit]

The typeid keyword is used to determine the class of an object at run time. It returns a reference to std::type_info object, which exists until the end of the program.[3] The use of typeid, in a non-polymorphic context, is often preferred over dynamic_cast<class_type> in situations where just the class information is needed, because typeid is a constant-time procedure, whereas dynamic_cast must traverse the class derivation lattice of its argument at runtime.[citation needed]Some aspects of the returned object are implementation-defined, such as std::type_info::name(), and cannot be relied on across compilers to be consistent.

Objects of class std::bad_typeid are thrown when the expression for typeid is the result of applying the unary * operator on a null pointer. Whether an exception is thrown for other null reference arguments is implementation-dependent. In other words, for the exception to be guaranteed, the expression must take the form typeid(*p) where p is any expression resulting in a null pointer.

Example[edit]

#include <iostream>    // cout
#include <typeinfo> // for 'typeid' class Person
{
public:
virtual ~Person() {}
}; class Employee : public Person
{
}; int main()
{
Person person;
Employee employee;
Person* ptr = &employee;
Person& ref = employee;
// The string returned by typeid::name is implementation-defined
std::cout << typeid(person).name() << std::endl; // Person (statically known at compile-time)
std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time)
std::cout << typeid(ptr).name() << std::endl; // Person* (statically known at compile-time)
std::cout << typeid(*ptr).name() << std::endl; // Employee (looked up dynamically at run-time
// because it is the dereference of a
// pointer to a polymorphic class)
std::cout << typeid(ref).name() << std::endl; // Employee (references can also be polymorphic) Person* p = nullptr;
try
{
typeid(*p); // not undefined behavior; throws std::bad_typeid
}
catch (...)
{
} Person& pRef = *p; // Undefined behavior: dereferencing null
typeid(pRef); // does not meet requirements to throw std::bad_typeid
// because the expression for typeid is not the result
// of applying the unary * operator
}

Output (exact output varies by system):

Person
Employee
Person*
Employee
Employee https://en.wikipedia.org/wiki/Run-time_type_information

Run-time type information--RTTI的更多相关文章

  1. RTTI(Runtime Type Information )

    RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...

  2. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information

    Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...

  3. Dynamic type checking and runtime type information

    动态类型的关键是将动态对象与实际类型信息绑定. See also: Dynamic programming language and Interpreted language Dynamic type ...

  4. TIJ——Chapter Fourteen:Type Information

    Runtime type information(RTTI) allows you to discover and use type information while a program is ru ...

  5. C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释

    RTTI(RunTime Type Information)执行时类型信息 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details ...

  6. RTTI (Run-time type information) in C++

    In C++, RTTI (Run-time type information) is available only for the classes which have at least one v ...

  7. (TODO:)下载图片,报错:warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.

    想使用NSInvocationOperation下载图片,然而并没有下载下来, NSData为nil, 还有报错:(打断点就报错) warning: could not load any Object ...

  8. c++对象模型和RTTI(runtime type information)

    在前面已经探讨过了虚继承对类的大小的影响,这次来加上虚函数和虚继承对类的大小的影响. 先来回顾一下之前例子的代码: #include <iostream> using namespace ...

  9. SQL Server get SP parameters and get output fields type information

    Summary 本文主要介绍一下,SQL里面的两个很实用的两个操作: 获取存储过程的参数信息 SELECT * FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPE ...

  10. coding++:error Could not read JSON: Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class java.lang.Object

    Spring源码中是使用容器中的ObjectMapper对象进行序列化和反序列化. 当我们将自定义的ObjectMapper对象放入IOC容器中后,会自动覆盖SpringBoot自动装载的Object ...

随机推荐

  1. JS 垃圾回收机制

    [转自]:https://segmentfault.com/a/1190000018605776 垃圾回收 JavaScript 中的内存管理是自动执行的,而且是不可见的.我们创建基本类型.对象.函数 ...

  2. PHP 时间处理

    1:获取当前日期格式时间 date("Y-m-d H:i:s");    2:转化为时间戳 strtotime( date("Y-m-d") ) 3:转化为日期 ...

  3. 路飞学城Python-Day137

    django项目二 个人博客系统 github地址:https://github.com/pandaboy1123/cnblog

  4. 常用css和js内容

    1.让一个200x200的div在不同分辨率屏幕上下左右居中. <div class="box"></div> <style type="t ...

  5. SQL中group by的理解

    1.group by A,B,C的分组顺序与汇总: group by A,B,C的分组顺序与order by A,B,C的排序一样.即先按A,如果A一样,则再按B,以此类推. 而数据将在最后指定的分组 ...

  6. C#通过SendMessage发送消息,改变其他程序的下拉框控件(ComboBox)的值

    IntPtr cbh= new IntPtr(handle); //ComboBox的句柄 SendMessage(cbh, 0x014D, new IntPtr(-1), "需要选中的下拉 ...

  7. django-5-自定义模板过滤器及标签

    <<<代码布局(自定义的代码放哪里)>>> (1)某个app特有的  1.一般放app目录下 固定名为templatetags 的python文件夹里鸭,如果是别的 ...

  8. TensorFlow 制作自己的TFRecord数据集

    官网的mnist和cifar10数据之后,笔者尝试着制作自己的数据集,并保存,读入,显示. TensorFlow可以支持cifar10的数据格式, 也提供了标准的TFRecord 格式,而关于 ten ...

  9. String 字符串的追加,数组拷贝

    package chengbaoDemo; import java.util.Arrays; /** *需求:数组的扩容以及数据的拷贝 *分析:因为String的实质是以字符数组存储的,所以字符串的追 ...

  10. HDU1850 Being a Good Boy in Spring Festival

    /* HDU1850 Being a Good Boy in Spring Festival http://acm.hdu.edu.cn/showproblem.php?pid=1850 博弈论 尼姆 ...