禁用某些类别的RTTI [英] Disable RTTI for some classes
问题描述
我有一个C ++转换单元,我需要在其中的两个类中禁用RTTI,但没有别的.是否有类似#pragma rtti(off)
之类的东西或我可以使用的东西?
I've got a C++ translation unit and I need to disable RTTI for two classes in it, but nothing else. Is there something like #pragma rtti(off)
or something that I can use?
我只需要为该类禁用RTTI.我不会抛出或捕获或dynamic_cast或此类之类的东西,因此我根本不需要RTTI.它的方法的实现当然需要在RTTI上进行编译,因为它们确实会引发异常,这只是我需要抑制的一个typeinfo对象的生成.
I need to disable RTTI for that class only. I do not throw or catch or dynamic_cast or anything this class, so I simply don't need the RTTI for it. The implementation of it's methods certainly need to be compiled with RTTI on, as they can indeed throw exceptions, it's just the generation of this one typeinfo object that I need to suppress.
推荐答案
要在g ++中禁用特定类的RTTI,别无其他(在有限的测试用例上进行测试,请谨慎行事):
To disable RTTI in g++ for a particular class and nothing else (tested on a limited test case, exercise caution):
- 将类定义移动到单独的头文件中.
- 向您的班级添加一个新的虚函数
virtual void nortti();
.使其成为第一个虚拟功能. - 将其实现放到单独的源文件中.用
fno-rtti
编译该文件. - 正常编译其余的类实现.
- Move the class definition to a separate header file.
- Add a new virtual function
virtual void nortti();
to your class. Make it the very first virtual function. - Put its implementation to a separate source file. Compile this file with
fno-rtti
. - Compile the rest of the class implementation normally.
这篇关于禁用某些类别的RTTI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!