何时为类创建 v 表?

发布于 2024-10-03 06:32:10 字数 331 浏览 7 评论 0原文

我知道,如何实现虚函数调用解析不是 C++ 标准的一部分,也没有提到任何有关 vptr 或 v-table 的内容,但让我在这里问这个问题。

我听说 v-table 是编译器用来实现虚拟函数调用解析的常用技术。我对此的理解是每个类、每个进程只需要虚拟表。

我想知道的是,什么时候为类创建 v-table?
是在进程空间中第一次创建给定类型的类(需要 v 表)时吗?
该进程空间中所有其他随后创建的该类型的对象是否引用已创建的 v 表?
这个v表什么时候会被删除呢?

如果这是一个过于主观或讨论类型的问题,我很抱歉,但这些问题在我脑海中徘徊了一段时间,我觉得在这里问它是可以的。

I know that, how to implement virtual function call resolution is not part of C++ standrads nor it says anything about vptr or v-table, but let me ask this question here.

I've heard that v-table is a common technique used by compilers to implement virtual function call resolution. My understading about this is that there is only virtual table is needed per class, per process.

What I am wondering is, when is the v-table is created for a class?
Is it When the class of a given type (which needs a v-table) is created in a process space for the first time?
All other subsequently created objects of that type in that process space, refers to the v-table that is already created?
When would this v-table will be deleted?

I am sorry if this is too subjective or discussion type of question, but these questions lingers in my mind for a while and I feel its OK asking it here.

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

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

发布评论

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

评论(4

寂寞陪衬 2024-10-10 06:32:10

v表是静态分配的,永远不会被删除,也不会显式分配。任何给定特定对象内的指针都是常量。

The v-table is statically allocated and is never deleted, nor is it explicitly allocated. The pointers within any given specific object are constants.

微暖i 2024-10-10 06:32:10

C++ 常见问题解答提供了以下内容的简化说明: vtable机制。您应该阅读一下,尽管您可能需要阅读特定的编译器文档以获取更多详细信息。

从我的角度来看,最重要的想法是:

  • 类型的 vtable 是静态的,并且在编译时构建
  • 每个类型实例都包含一个指向该表的指针
  • 因为该指针是在构建时初始化的,所以永远不应该从构造函数中调用虚拟成员函数

The C++ FAQ provides a simplified explanation of the vtable mechanism. You should give it a read, although you will probably have to go through your particular compiler documentation for more details.

The most important ideas from my point of view :

  • The vtable for a type is static and built at compile time
  • Each of the type instances contains a pointer to this table
  • Because this pointer is initialized at construction time, a virtual member function should never be called from the constructor
流星番茄 2024-10-10 06:32:10

vtable 是静态数据,因此在加载时立即可用。顺便说一句,它通常捆绑在编译单元中,其中包含类上第一个非内联虚函数的定义(当只有一个内联虚函数时,启发式会导致问题)。

The vtable is static data so available immediately at load. BTW, it is usually bundled in the compilation unit which contains the definition for the first non-inline virtual function on the class (and that heuristic leads to problem when there is only one virtual function which is inline).

若能看破又如何 2024-10-10 06:32:10

我相信这都是由实现定义的,因此很难对这个问题给出通用的答案。我相信虚函数表应该是某种静态类成员。

I believe it's all implementation defined, so it's difficultto give a universal answer to this question. I believe the vtable should be a some sort of a static class member.

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