Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation principle of RTTI of C++ object model?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what is the implementation principle of the RTTI of C++ object model". In the operation of actual cases, many people will encounter this dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

RTTI is an acronym for Runtime Type Identification, meaning runtime type recognition. C++ introduced this mechanism so that the program can get the actual type of the object referred to by the pointer or reference according to the pointer or reference of the base class at runtime. But now the type recognition of RTTI is no longer limited to this, it can also identify the corresponding types of variables of all basic types (int, pointers, etc.) through typeid operators.

C++ provides RTTI through the following two operations:

Typeid operator, which returns the actual type of its expression or type name.

The dynamic_cast operator, which safely converts a pointer or reference of a base class to a pointer or reference of a derived class type.

The implementation of these two operations is described in detail below.

Note the test environment for all the test code is: 32-bit Ubuntu 14.04 gears + 4.8.2, if you test in different environments, the results may be different.

1. Typeid operator

The typeid operator, followed by a type name or an expression, returns a const reference to an object of type std::tpeinf. Type_info is a class in std that records type-related information. The definition of the class type_info is roughly as follows:

Class type_info {public: virtual ~ type_info (); bool operator== (const type_info&) const; bool operator = (const type_info&) const; bool before (const type_info&) const; const char* name () const; private: type_info (const type_info&); type_info& operator= (const type_info&); / / data members}

As for the data members section, different compilers will vary, but must provide a minimum amount of information that is the real name of class and some sort algorithm between type_info objects (provided through the before () member function), as well as some form of descriptor that represents the type of the explicit class and any subtypes of the class.

You can also see from the above definition that type_info provides equality comparison between two objects, but users cannot define an object of type_info themselves, but can only use an object of type_info by returning a const reference of an object through the typeid operator. Because it declares only one constructor (copy constructor) and is private, the compiler does not synthesize any constructors, and the assignment operator is private. These two operations completely prohibit users from defining and copying type_info objects, and users can only use the class through pointers or references to type_info objects.

Let's talk about typeid's handling and implementation of statically typed expressions and dynamically typed expressions.

1) typeid recognizes static types

When the operands in typeid are one of the following cases, the typeid operator indicates the static type of the operands, that is, the type at compile time.

Type name

A basic type of variable

A specific object

Dereferencing of a pointer to a class object that does not contain a virtual function

A reference to a class object that does not contain a virtual function

The static type does not change during the running of the program, so it is not necessary to calculate the type while the program is running, and the type information can be deduced from the static type of the Operand at compilation time. For example, in the following code snippet, the operands in typeid are of static type:

Class X {. / / with virtual function}; class XX: public X {. / / has virtual function}; class Y {. / / No virtual function}; int main () {int n = 0; XX xx; Y y; Y * py = & y; / / int and XX are both type names cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report