何时为类创建 v 表?
我知道,如何实现虚函数调用解析不是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.
C++ 常见问题解答提供了以下内容的简化说明: 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 :
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).
我相信这都是由实现定义的,因此很难对这个问题给出通用的答案。我相信虚函数表应该是某种静态类成员。
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.