Run-time type information--RTTI
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的更多相关文章
- RTTI(Runtime Type Information )
RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...
- 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 ...
- Dynamic type checking and runtime type information
动态类型的关键是将动态对象与实际类型信息绑定. See also: Dynamic programming language and Interpreted language Dynamic type ...
- TIJ——Chapter Fourteen:Type Information
Runtime type information(RTTI) allows you to discover and use type information while a program is ru ...
- C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释
RTTI(RunTime Type Information)执行时类型信息 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details ...
- 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 ...
- (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 ...
- c++对象模型和RTTI(runtime type information)
在前面已经探讨过了虚继承对类的大小的影响,这次来加上虚函数和虚继承对类的大小的影响. 先来回顾一下之前例子的代码: #include <iostream> using namespace ...
- SQL Server get SP parameters and get output fields type information
Summary 本文主要介绍一下,SQL里面的两个很实用的两个操作: 获取存储过程的参数信息 SELECT * FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPE ...
- 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 ...
随机推荐
- 日常记录-Pandas Cookbook
Cookbook 1.更新内容 2.关于安装 3.Pandas使用注意事项 4.包环境 5.10分钟Pandas初识 6.教程 7.Cookbook 8.数据结构简介 9.基本功能 10.使用文本数据 ...
- 取得Linux系统的各种统计信息
本文基于Linux 2.6.x内核 一.取得CPU信息(相关文件/proc/stat) 在一个系统中的/proct/stat文件内容如下 $ cat /proc/stat cpu 1039426 17 ...
- 探索JS引擎工作原理 (转)
这篇文章从相对底层的角度介绍了js引擎的工作 引入了 静态作用域 执行环境上下文(context) 等概念 , http://www.cnblogs.com/onepixel/p/5090799.ht ...
- iframe嵌入的子页面如何刷新父窗口
iframe中刷新父页面方法及一些按钮刷新代码集合[原创+转]2009-07-23 11:12a页面里iframe了个b页面,我想实现在b页面里一个按钮,一按就刷新a页面,也就是父页面,不是只刷新if ...
- LCIS 最长公共上升子序列问题DP算法及优化
一. 知识简介 学习 LCIS 的预备知识: 动态规划基本思想, LCS, LIS 经典问题:给出有 n 个元素的数组 a[] , m 个元素的数组 b[] ,求出它们的最长上升公共子序列的长度. 例 ...
- [SDOI2016]数字配对(费用流+贪心+trick)
重点是如何找到可以配对的\(a[i]\)和\(a[j]\). 把\(a[i]\)分解质因数.设\(a[i]\)分解出的质因数的数量为\(cnt[i]\). 设\(a[i]\geq a[j]\) 那么\ ...
- nyoj48-小明的调查作业
48-小明的调查作业 内存限制:64MB时间限制:1000msSpecial Judge: No accepted:3submit:4 题目描述: 小明的老师布置了一份调查作业,小明想在学校中请一些同 ...
- hive 自定义函数(udf)
1.解压hive的压缩包 2.新建java项目,将hive压缩包里面lib文件夹的jar包导入新建的项目 3.编写java程序 package udfdemo; import org.apache.h ...
- Python Django log日志
log日志 log开发日志 一.创建项目 1.python -m venv ll_env # 创建虚拟环境 2.source ll_env/bin/activate # 激活虚拟环境 3.pip in ...
- [SharePoint2010开发入门经典]9创建更好的用户体验----silverlight
本章概要: 1.了解Silverlight 2.理解为什么使用Silverlight 3.介绍如何集成SharePoint和Silverlight