Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top