gpt4 book ai didi

C++ : How can I know the size of Base class SubObject?

转载 作者:太空狗 更新时间:2023-10-29 20:31:25 27 4
gpt4 key购买 nike

.

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com