Click here to Skip to main content
15,890,609 members

Response to: Virtual table creation

Revision 2
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
Posted 25-Dec-12 20:39pm by Sergey Alexandrovich Kryukov.
Tags: