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


------------------越是喧嚣的世界,越需要宁静的思考------------------ 合抱之木,生于毫末;九层之台,起于垒土;千里之行,始于足下。 积土成山,风雨兴焉;积水成渊,蛟龙生焉;积善成德,而神明自得,圣心备焉。故不积跬步,无以至千里;不积小流,无以成江海。骐骥一跃,不能十步;驽马十驾,功在不舍。锲而舍之,朽木不折;锲而不舍,金石可镂。蚓无爪牙之利,筋骨之强,上食埃土,下饮黄泉,用心一也。蟹六跪而二螯,非蛇鳝之穴无可寄托者,用心躁也。