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 ...
随机推荐
- 对于开启tomcat后无法登陆index.xml的新解决方法
首先这个问题是针对tomcat路径什么的都正确,但是就是无法登陆index.xml 如上图,之前忘了写<packaging>war</packaging>所以无法登陆index ...
- lsync 负载实现代码双向同步
参考文件: https://www.zyku.net/centos/1713.html 检查错误命令: lsyncd -pidfile /tmp/lsyncd.pid /etc/lsyncd.con ...
- Eclipse maven工程 Missing artifact com.sun:tools:jar:1.7.0:system 解决方法
解决方案一:通过maven取运行时参数,eclipse提供的环境变量,基本类似System.getProperty("java.home") <dependency> ...
- C语言基础 (8) 常用字符串处理函数
复习 如何调用库函数(别人写好的函数) 1) 头文件:包含指定的头文件,头文件主要包含此函数的声明 2) 函数名字:函数名字必须和头文件声明的名字一样 字符串常用处理函数: 1 ...
- 自己对WEBGL坐标系的转换过程的理解【如图】
- php多维数组的指定单个字段排序
多维数组如何根据指定键值?比如现在有数组结构如下: ,,,,,'subject' => 'math'), 1 => array('name' => '3班','avgScore'=& ...
- Maven 从安装到环境配置到项目搭建
maven是基于项目对象模型(pom),可以通过一小段的描述信息来管理项目的构建,报告和文档的软件项目管理工具. Maven是构建项目的管理工具,白话就是说:“Maven的核心功能便是合理叙述项目间的 ...
- PHP 7.1.15安装zabbix-3.2.6出现问题解决
出现问题,显示 A non well formed numeric value encountered [zabbix.php:21 → require_once() → ZBase->run( ...
- 2019-04-03 SQL Group By某列,预先对该列进行一个预处理,提炼出共有的信息,即关键字case when 列名什么条件 then 赋值 else 赋值 end as 新列名
select sum(发行金额) from( select PoolNameFormat,count(cast(ItemValue as decimal(19,4))) as 发行笔数,sum(cas ...
- deep learning 经典网络模型之Alexnet、VGG、Googlenet、Resnet
CNN的发展史 上一篇回顾讲的是2006年Hinton他们的Science Paper,当时提到,2006年虽然Deep Learning的概念被提出来了,但是学术界的大家还是表示不服.当时有流传的段 ...