Disable RTTI for some classes(为某些类禁用 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):
- 将类定义移动到单独的头文件中.
- 在你的类中添加一个新的虚函数
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为某些类禁用 RTTI


- C语言手把手带你掌握带头双向循环链表 2023-04-03
- C语言qsort()函数的使用方法详解 2023-04-26
- Easyx实现扫雷游戏 2023-02-06
- ubuntu下C/C++获取剩余内存 2023-09-18
- Qt计时器使用方法详解 2023-05-30
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- C语言详解float类型在内存中的存储方式 2023-03-27
- C++ 数据结构超详细讲解顺序表 2023-03-25
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09