current position:Home>[C / C + + learning] 14. RTTI

[C / C + + learning] 14. RTTI

2022-01-26 21:42:39 mb61b856e04bb98


RTTI(Runtime TypeIdentification)

Provides methods for determining object types and converting pointer or reference types at run time .  Relevant contents include class name , Data member name and type , Function name and type, etc .

When most people spread their design ideas to experts , Both advocate using virtual member functions in design and development instead of RTTI Mechanism , However, in many cases, virtual member functions cannot overcome their limitations . Whenever it comes to dealing with heterogeneous containers and the underlying class hierarchy , It is inevitable to judge the object type dynamically , That is, dynamic type detection . stay C++ Two operators are provided in typeid and dynamic_cast.

typeid    You can get the runtime type information associated with an object . He returned to type_info type , This type describes type information .

Typeid Operator is used to obtain the runtime information of a type or object , as follows :

Typeid(type)

Typeid(object)

among type It can be a class type with virtual functions , It can also be a class type without virtual functions

1、  There are virtual functions

A *a = new B;

Typeid(a)==typeid(A*); and typeid(*a)==typeid(A)  All back to true                                                                                                                         

2、  No virtual function

A *a = new B;

Typeid(a)==typeid(A*) and typeid(*a)==typeid(A) All back to true


Typeid Is in typeinfo.h Header file , Look at the code below

A *a = new B;

Cout << typeid(*a).name() <<endl;

Name Function can get the pointer a Object type pointed to , Because the pointer a Point to a B Object of type , When more than one is executed, it will output B.

If a yes NULL, Then it will throw bad_typeid abnormal .


Typeid and dynamic_cast difference :

1、  dynamic_cast Operator can only handle objects , and typeid Operators can take types and objects as operands , Where objects can make basic types , Including plastic surgery , floating-point ,char etc.

2、  typeid() and dynamic_cast<>() Functions can only get type information at runtime , Instead of getting... At static compilation time .

3、  typeid() Function is not a template function

4、  typeid() and dynamic_cast<>() Function receives a base class pointer or reference , The pointer or reference can point to a derived class object .

5、  typeid() Function returns the class name , and dynamic_cast<>() Function is used to pass the class name .

6、  Use typeid() Function can find the exact type of the object , But you can't convert the pointer ,dynamic_cast<>() The function can convert the pointer , But the exact type of the object cannot be determined .




perform dynamic_cast Operator requires extra running time , In order to avoid performance loss, we usually use static_cast Operator .   Although the use of static_cast Operator is faster than dynamic_cast Operator , however static_cast Operators can be dangerous and cause errors when used for downward transformation .  If you must use it static_cast Operator , Then it is necessary to ensure the security of downward transformation .

Use static_cast Operators do not guarantee the effectiveness of the transformation ,static_cast Operator only performs the required pointer operation , To ensure use static_cast It is safe for operators to transition down , Users must test every transformation they want to perform . If you use dynamic_cast Operators do not require user testing , because dynamic_cast When the operator fails to execute, it will return a null pointer or throw an exception .


It should be noted that :

By default , The compiler is off RTTI Of , The goal is to eliminate performance overhead , If used in the program RTTI, Then enable it before compiling RTTI.




The most basic RTTI Include :

1、  Class recognition : Class name or ID

2、  Inheritance relationships : Support runtime down conversion , Dynamic conversion

3、  Object structure : Type of attribute 、 Name and location

4、  Member functions : Type of function 、 Name and parameter type

5、  Can get the objects instantiated by the class


RTTI Common applications :   exception handling , Dynamic transformation , Multiple file integration , object IO

1、  exception handling : need RTTI  For example, class name, etc

2、  Dynamic transformation : In class , Downward conversion requires class inheritance RTTI

3、  Multiple file integration : When an object in one program needs to use an object in another program , In a normal C++ In the program , The common solution is to include the class definition of the required object in the source code , Integrate the current file with the files it contains at compile time , But you can't recompile again , Therefore, we can only rely on RTTI.

4、  object IO:C++ take IO Object and its related exclusive class name, etc RTTI Keep the content intact , You can rely on these when reading objects RTTI Allocate memory space for content objects .




















2012/10/14

jofranks Yu Nanchang





















copyright notice
author[mb61b856e04bb98],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262142345729.html

Random recommended