使用typeid实现RTTI [英] Implementation of RTTI using typeid

查看:144
本文介绍了使用typeid实现RTTI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是研究高级C ++主题的新手,所以如果问题听起来太明显,请原谅我.

I am a newbie in the study of advanced C++ topics so please pardon me if the question sounds too obvious.

我一直在阅读各种方法,通过这些方法,我们可以在C ++中(通常称为RTTI)在运行时获取对象类型的信息.

I have been reading about the various methods by which we can get the information of the type of the object at runtime in C++, which is generally referred to as RTTI.

但是,我对它如何工作感到困惑. 我已经阅读了一些在解释RTTI时经常提到的内容.其中之一是使用dynamic_cast<>将对象动态转换为其他对象.另一个是使用typeid在运行时确定对象的类型.

But, I am confused as to how does it work. I have read some of things that are often mentioned when RTTI is being explained. One of them is the use of dynamic_cast<> to cast an object to some other object dynamically. Another is the use of typeid to determine the type of the object at runtime.

我想知道使用typeid是否是在C ++中实现RTTI的正确方法,如果是,那么typeid运算符实际上如何能够在运行时确定对象的类型(它是否分析类型的位模式?内存中有对象blob吗?)

I want to know whether using typeid is the correct way to implement RTTI in C++ and if yes, then how is typeid operator in fact able to determine the type of the object at run time ( Does it analyse the bit patterns of the object blob in memory? )

如果使用typeid不是正确的方法,请提出其他实现方法.

If using typeid is not the correct way, then please suggest some other method for its implementation.

推荐答案

重要提示:
理想情况下,如果需要标识对象的类型,则需要重新设计,因为很可能您错过了那里的东西,并且违反了

Important:
Ideally, if you need to identify the type of an object you need to revisit your design because most likely you missed something there and you are violating the SOLID rules of OOP.

C ++标准提供了dynamic_casttypeid作为确定对象的 type 的两种方式.两者都有其优点和局限性.他们如何识别 type 是实现相关的详细信息,但通常通过在对象的vtable中维护指向类型信息结构的指针来实现.如果您完全不知道什么是vtable,请在 此处 .

C++ standard provides dynamic_cast and typeid as two ways of determining the type of an object. Both have their advantages and limitations. How they identify the type is an implementation dependent detail but typically they do so by maintaining a pointer to the type information structure in the vtable of an object. If you are completely unaware of what a vtable is, Marshal Clines C++ Faq provides a good explanation here.

您可以在以下位置找到大多数编译器使用的实现详细信息: 有关C ++性能的技术报告

You can find the implementation detail used by most compilers in,
Technical Report on C++ Performance

相关摘录:

5.3.7类型信息

5.3.7 Type Information

给出一个多态类(具有至少一个虚函数的类)的对象, type_info对象可以通过使用typeid运算符获得.在 原则上,这是一个简单的操作,涉及查找虚拟功能表, 通过找到该对象所属的最派生的类对象,然后从该对象的虚函数表中提取指向type_info对象的指针(或 等效).

Given an object of a polymorphic class (a class with at least one virtual function), a type_info object can be obtained through the use of the typeid operator. In principle, this is a simple operation which involves finding the virtual function table, through that finding the most-derived class object of which the object is part, and then extracting a pointer to the type_info object from that object’s virtual function table (or equivalent).


5.3.8动态投射

5.3.8 Dynamic Cast

给出一个指向多态类对象的指针,将其转换为另一个基数的指针 可以使用dynamic_cast完成同一派生类对象的子对象.在 原则上,此操作涉及通过发现虚拟功能表 对象所属的最派生的类对象,然后使用类型信息 与该对象相关联,以确定是否允许进行转换(转换),最后 执行此指针的任何必需的调整.原则上,这种检查 涉及遍历描述最派生的基类的数据结构 班级.因此,dynamic_cast的运行时成本可能取决于相对位置 在所涉及的两个类的类层次结构中.

Given a pointer to an object of a polymorphic class, a cast to a pointer to another base subobject of the same derived class object can be done using a dynamic_cast. In principle, this operation involves finding the virtual function table, through that finding the most-derived class object of which the object is part, and then using type information associated with that object to determine if the conversion (cast) is allowed, and finally performing any required adjustments of the this pointer. In principle, this checking involves the traversal of a data structure describing the base classes of the most derived class. Thus, the run-time cost of a dynamic_cast may depend on the relative positions in the class hierarchy of the two classes involved.

这篇关于使用typeid实现RTTI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆