虚函数表指针的大小是多少?

发布于 11-29 20:10 字数 172 浏览 1 评论 0原文

在单继承中,指向虚拟表的指针的大小是否始终等于 void* 的大小?你说,

class vft { virtual ~vft(); }
assert (sizeof(vft) == sizeof(void*));

这个断言总是正确的吗?

In single inheritances, is the size of a pointer to virtual table always equal to the size of a void*? Say,

class vft { virtual ~vft(); }
assert (sizeof(vft) == sizeof(void*));

Would that assertion always be true?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆依然2024-12-06 20:10:57

不,虚拟主义是由实现定义的。它是编译器实现细节。
所以你不能说这永远是真的。

另外,您应该避免编写任何假定标准保留的细节作为编译器实现细节的代码(例如问题中的代码),因为这会使您的代码不能 100% 跨编译器和可移植性。在某些编译器上甚至可能会严重失败。

No, Virtualism is implementation defined. It is an compiler implementation detail.
So you cannot say that will be true always.

Also, You should refrain yourself from writing any code(like the one in Question) that assumes an detail that is left open by the Standard as an compiler implementation detail, Because that makes your code not 100% portable across compilers & might even fail drastically on some compilers.

我三岁2024-12-06 20:10:57

C++ ISO 标准首先没有提及虚拟函数表指针。编译器可以遵循此机制来支持运行时多态性,或者可以提出任何其他甚至不涉及 vptr 的机制。这完全取决于编译器编写者。既然标准没有提及 vptr 的任何内容,那么它如何说明它的大小呢?决不。结论是:你正在做的(或假设的)并没有得到语言的保证。然而,对于编译器来说,它可能总是正确的。

顺便说一句,对于您的编译器,您如何得出 sizeof(vft) 将等于 sizeof(vptr) 的结论?很可能是 sizeof(vft) > sizeof(vptr)。但我并不这么说。

The C++ ISO Standard says nothing about virtual function table pointer in the first place. A Compiler may follow this mechanism to support runtime-polymorphism or can come up with any other which doesn't even involve vptr. Its entirely upto the compiler writers. Since the Standard doesn't say anything about vptr, how can it say about its size? No way. The conclusion is: what you're doing (or assuming) isn't gauranteed by the language. However, for a compiler, it might be always true.

As a sidenote, for your compiler, how can you conclude that sizeof(vft) will be equal to sizeof(vptr)? It could very well be that sizeof(vft) > sizeof(vptr). I don't claim that though.

撩心不撩汉2024-12-06 20:10:57

这取决于实现。

This is implementation-dependent.

暖伴2024-12-06 20:10:57

除了其他人已经指出的事实之外,我觉得该标准确实提供了一些保证。即,对于 class vft { void* a, b;虚拟~vft(); } 然后 sizeof(vft) > sizeof(void*)。我很确定该标准至少保证了这一点,无论是否有虚拟函数。

Aside from the facts that everyone else already pointed out, I feel like the standard does give some guarantees. Namely, for class vft { void* a, b; virtual ~vft(); } then sizeof(vft) > sizeof(void*). I'm pretty sure the standard guarantees that much at least, virtual functions or no.

小清晰的声音2024-12-06 20:10:57

“这个断言永远都是真的吗?”取决于你所说的“总是”是什么意思。

正如其他人已经指出的那样,即使同一编译器的不同版本,这个实现细节也可能会发生变化。 C++ 标准对此没有任何说明,因此您不能依赖它。

另一方面,我从来没有见过一个编译器不是这样的。

"Would that assertion always be true?" Depends on what you mean by "always".

As others have already pointed out, this is an implementation detail that is subject to change even with different versions of the same compiler. The C++ standard says nothing about it, so you can't rely on it.

On the other hand, I've never seen a compiler where this wouldn't be true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文