虚拟表创建 [英] Virtual table creation

查看:72
本文介绍了虚拟表创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我知道在同一主题上有很多现有的论坛。但是他们不清楚。

所以我的问题是..

什么时候用c ++创建的虚拟表是编译时还是运行时?



在一些论坛中提到,在编译时只会创建表的布局(这意味着什么),并且在运行时它会在表中获取条目。 br />


有人可以给我简要的概述吗?



提前谢谢

Hi All,

I know there are so many existing forums on the same topic. But they are not clear.
so my question is..
When is the virtual table created in c++ whether compile-time or run-time?

In few of the forums it is mentioned that,at compile time only the layout of the table gets created (what does it means) and at run time it gets entries in the table.

Can someone give me brief overview of this?

Thanks in advance

推荐答案

本文讨论虚拟表 - C中的多态性 [ ^ ]
This article talks about virtual tables - Polymorphism in C[^]


这么简单的问题,但我知道甚至很有经验的开发人员也很困惑。



答案取决于你所说的虚拟桌子 。它确实作为目标文件中的图像存在,后来它来到可执行文件并最终加载到内存中。在所有阶段,它实际上都是作为一些神器而存在。



首先要理解的是:每个类只有一个虚拟表。此类的所有实例(对象)共享虚拟表的相同实例。如果你想一想OOP如何在这些虚拟表的帮助下工作,你就会明白这足以实现OOP功能:后期绑定,因此也就是多态。



其他一切都是对这一事实的理解,以及对代码生命周期的理解。编译创建一个目标文件,虚拟表已经以某种形式存在,以及构成该类的其他所有内容。然后链接器将它们放在一起,因为某些类(甚至是相同的类层次结构)可能出现在不同的对象或静态库文件中。在完全链接的表单中,所有类都进入可执行文件(.EXE,.DLL,.so或任何东西,PE,elf或任何其他可执行文件格式)。因此,虚拟表已经存在。然后系统加载程序将所有代码排列在内存中。这不是真正的运行时,而是之前发生的事情,我称之为加载时间。然后,即使在运行期间可能会发生某些类预先详细说明(这取决于实现),虚拟表本身也已存在。



- SA
Such a simple question, but I knew even pretty experienced developers who have been confused.

The answer depends on what you call a "virtual table". It does exist as the image in the object file, and later it comes to the executable file and finally loaded to the memory. In all stages, it is actually exists as some artifact.

First thing to understand is: there is no more than one virtual table per class. All the instances (objects) of this class share the same instance of the virtual table. If you think about how OOP works with the help of those virtual table, you will understand that this is quite enough to implement the OOP functionality: late binding and hence polymorphism.

Everything else comes with understanding of this fact, and understanding of the code life cycle. Compilation creates an object file, and the virtual table is already there in some form, as well as everything else which makes the class. Then the linker put it all together, as some classes (even of the same class hierarchy) may appear in different object or static library files. In the fully linked form, all classes make their way to the executable file (.EXE, .DLL, .so or anything, PE, elf, or any other executable file format). So, the virtual tables exist already. And then the system loader arranges all the code in memory. This is not really runtime, but something which happens before, I would call it "load time". Then, even if some class pre-elaboration may take place during runtime (this is implementation-dependent), the virtual tables themselves are already there.

—SA


编译器已经需要知道哪个函数的地址在类的v-table的单元号n中。否则它无法生成调用该函数的代码。所以基本上你的问题的答案是:V-tables是在编译时准备的。所以编译器例如知道函数MyFuncXyz位于V表单元格3中。



但是:函数的确切地址有时在编译时是未知的,例如当函数驻留在另一个DLL中时。在运行时加载DLL时,加载器可能会发现它必须重新定位到另一个基址。在这种情况下,程序中引用该DLL中的函数的所有单元格都将相应地进行修改。因此,V-table的单元格也会在重定位过程中更新。



这就是为什么有些人说V-tables正在准备中表格在编译时,但在运行时完成。



我希望能为你澄清一些事情。
The compiler already needs to know which function''s address is in cell number n of the v-table of a class. Otherwise it couldn''t generate code to call that function. So basically the answer to your question is: V-tables are prepared at compile time. So the compiler for instance knows that function "MyFuncXyz" sits in V-table cell 3.

But: The exact address of a function is sometimes not known at compile time, e.g. when the function resides in another DLL. When the DLL is loaded at runtime the loader may find that it has to be relocated to another base address. In that case, all cells in your program that refer to a function in that DLL will be modified accordingly. And so, also the cells of a V-table get updated in that relocation process.

That is why some people say that V-tables are being prepared "in some form" at compile time, but are being finished at runtime.

I hope that clarifies things for you.


这篇关于虚拟表创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆