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
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Hadoop environment setup (MySQL environment configuration)
- 10 minutes, using node JS creates a real-time early warning system for bad weather!
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- Anaconda can't open it
guess what you like
-
I don't know how to start this
-
Matlab simulation of transportation optimization algorithm based on PSO
-
MySQL slow log optimization
-
[Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
-
Popular Linux distributions for embedded computing
-
Suzhou computer research
-
After installing SSL Certificate in Windows + tomcat, the domain name request is not successful. Please answer!!
-
Implementation time output and greetings of jQuery instance
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
How to save computer research
Random recommended
- Springboot implements excel import and export, which is easy to use, and poi can be thrown away
- The final examination subjects of a class are mathematical programming, and the scores are sorted and output from high to low
- Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
- C + + recursive knapsack problem
- The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
- PostgreSQL statement query
- Ignition database test
- Context didn't understand why he got a high salary?, Nginxfair principle
- Bootstrap switch switch control user's guide, springcloud actual combat video
- A list that contains only strings. What other search methods can be used except sequential search
- [matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
- [matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
- Iinternet network path management system
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- If you want an embedded database (H2, hsql or Derby), please put it on the classpath
- When using stm32g070 Hal library, if you want to write to flash, you must perform an erase. If you don't let it, you can't write continuously.
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- 69. Sqrt (x) (c + + problem solving version with vs runnable source program)
- Fresh students are about to graduate. Do you choose Java development or big data?
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Titanic passenger survival prediction
- Vectorization of deep learning formula
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- For someone, delete return 1 and return 0
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- How does the database cow optimize SQL?
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Webpack packaging optimization solution
- 5. Operation element
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path
- How to solve the run-time exception of test times
- Detailed explanation of k8s management tool kubectl
- Android system view memory command