- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
.
Here我在讨论空基优化,以及 MSalters发表了这个有趣的评论:
No class can ever have sizeof(Class)==0, empty or not. But we're talking specifically over the size of an empty base class subobject. It doesn't need its own vtable, nor a vtable pointer. Assume the common layout of a vtable pointer at offset 0; that would cause the zero-sized base class subobject to share its vtable pointer with the derived class. No problem: those should be identical anyway, that's pretty much the point of virtual functions.
我的具体问题是:当我们使用 Empty Class 作为基类时,编译器可能会优化,也可能不会。我们如何确定它的实际作用?
一般来说,我们怎么知道基类子对象的大小呢?无论我们是否将其用作基础,基础子对象的大小是否相同?编译器是否仅使用空基类进行优化?
.
最佳答案
很好的答案。
顺便说一句,MS VC++ 和 G++ 编译器可以转储类布局供您研究。
使用 VC++ 只需运行 cl.exe /c /d1reportAllClassLayout <source>.cpp
这使用我在 1990 年编写的类和 vtable 布局转储代码来测试正确的布局对象、vtables、vbtables 等。
为了更好地理解 C++ 编译器如何在内存中布置对象,您可能会喜欢 http://www.openrce.org/articles/files/jangrayhood.pdf和 Stan Lippman 的书 Inside the C++ Object Model。
祝你黑客愉快!
关于C++ : How can I know the size of Base class SubObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4338966/
我是一名优秀的程序员,十分优秀!