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
Such a simple question, but I knew even pretty experienced developers who have been confused.—SA
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.
Edited 25-Dec-12 20:40pm
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.
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)