为某些类禁用 RTTI [英] Disable RTTI for some classes

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

问题描述

我有一个 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.我不 throw 或 catch 或 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):

  1. 将类定义移动到单独的头文件中.
  2. 在你的类中添加一个新的虚函数 virtual void nortti();.让它成为第一个虚函数.
  3. 将其实现放到一个单独的源文件中.使用 fno-rtti 编译此文件.
  4. 正常编译类实现的其余部分.
  1. Move the class definition to a separate header file.
  2. Add a new virtual function virtual void nortti(); to your class. Make it the very first virtual function.
  3. Put its implementation to a separate source file. Compile this file with fno-rtti.
  4. Compile the rest of the class implementation normally.

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

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