简体   繁体   English

为什么Qt在qobject_cast,事件类型等中重新实现RTTI?

[英]Why does Qt reimplement RTTI in qobject_cast, event type, etc?

为什么Qt会在QObject层次结构, QEvent等中重新实现一个自定义RTTI系统和它们自己的dynamic_cast

First of all, only a few class hierarchies in Qt actually need RTTI. 首先,Qt中只有几个类层次结构实际上需要RTTI。 When you generate embedded code you can save a whole bunch of code space by not emitting RTTI information. 生成嵌入式代码时,可以通过不发出RTTI信息来节省一大堆代码空间。 When building Qt and suitably written projects that use it, you can turn off RTTI in the compiler. 在构建Qt并适当编写使用它的项目时,可以在编译器中关闭RTTI。 You can't use dynamic_cast anymore, thus QObject hierarchy has its own qobject_cast , and QEvent uses explicit integer type tags. 您不能再使用dynamic_cast ,因此QObject层次结构具有自己的qobject_cast ,而QEvent使用显式整数类型标记。

A custom RTTI system for QObject hierarchy also allows dynamic creation of types from their metadata, as well of the metadata of new types that the compiler was unaware of. 用于QObject层次结构的自定义RTTI系统还允许根据其元数据以及编译器未意识到的新类型的元数据动态创建类型。 That's why QML can work, for example. 例如,这就是为什么QML可以工作的原因。 In Qt 5, this functionality is offered by the private QMetaObjectBuilder . 在Qt 5中,此功能由私有QMetaObjectBuilder The legacy way of creating dynamic signals and slots wasn't compatible with standard QObject::connect . 创建动态信号和插槽传统方法与标准QObject::connect不兼容。

Historically, with some compilers dynamic_cast failed across shared library boundaries. 从历史上看,对于某些编译器, dynamic_cast跨共享库边界失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM