C++ Standard Library<typeinfo>
In C++,<typeinfo>It is part of the standard library, providing runtime type identification (RTTI) functionality. RTTI allows programs to determine the type of an object at runtime. This is done by usingtypeidoperator andtype_infoimplemented by the class.
type_infoThe class is an abstract base class that provides an interface for type information. Every type has an associatedtype_infoobject, which can betypeidaccessed by operator.
Syntax
<typeinfo>Related main syntax:
typeidOperator: used to obtain the type information of an object.type_infoclass: a class that contains type information.
type information classtype_info
typeinfoThe header file provides runtime support for type information. It mainly contains two core components:std::type_infoclass andtypeidOperators.typeinfoIt allows the program to obtain the object's type information at runtime, which is very useful in polymorphic and type-safe code. The following is an introduction totypeinfoDetailed introduction:
std::type_info class
std::type_infoThe class istypeinfoThe core class of the header file, used to describe a type. It provides multiple member functions for querying type information. Commonly used member functions are as follows:
-
const char* name() const noexcept;Returns a C-string pointer to the type name. Note that this name is not necessarily a human-readable type name; its format is determined by the compiler implementation. -
bool before(const std::type_info& rhs) const noexcept;compares twotype_infoobjects in a certain order, returning whether the current object is beforerhsbefore. -
bool operator==(const std::type_info& rhs) const noexcept;compare twotype_infowhether the objects represent the same type. -
bool operator!=(const std::type_info& rhs) const noexcept;compare twotype_infowhether the objects represent different types.
typeid operator
typeidThe operator is used to obtain type information at runtime.typeidIt can operate on objects (pointers or references with polymorphic behavior) or types (without instantiating an object).
typeid(object): returns astd::type_infoobject, representingobjectthe dynamic type. IfobjectIf it is a polymorphic type (i.e., contains virtual functions), thentypeidit will return the actual type of the object.typeid(T): returns astd::type_infoobject, representing the typeT。
Example
Below is an example using<typeinfo>A simple example:
Example
#include <typeinfo>
class Base {
public:
virtual void show() { std::cout << "Base show" << std::endl; }
};
class Derived : public Base {
public:
void show() override { std::cout << "Derived show" << std::endl; }
};
int main() {
Base* basePtr = new Derived();
Base* basePtr2 = new Base();
std::cout << "Type of basePtr: " << typeid(*basePtr).name() << std::endl;
std::cout << "Type of basePtr2: " << typeid(*basePtr2).name() << std::endl;
if (typeid(*basePtr) == typeid(Derived)) {
std::cout << "basePtr is of type Derived" << std::endl;
} else {
std::cout << "basePtr is not of type Derived" << std::endl;
}
delete basePtr;
delete basePtr2;
return 0;
}
Output:
Type of basePtr: 9Derived // 注意:typeid的name()返回的类型名称可能因编译器而异 Type of basePtr2: 8Base // 同上 basePtr is of type Derived
Notes
- RTTI functionality depends on the compiler implementation, therefore
typeidThe type name returned by the operator may vary depending on the compiler. - Using RTTI may have a certain impact on program performance, because it requires type checking at runtime.
- RTTI is only applicable to polymorphic types, i.e., classes with virtual functions.
<typeinfo>It provides a method to identify object types at runtime, which is very useful for implementing polymorphism and type safety. However, developers should use RTTI with caution to avoid unnecessary performance overhead and potential type errors.