Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a vtable in C++ [duplicate]

Tags:

c++

vtable

Possible Duplicate:
why do I need virtual table?

What is a vtable in C++?

So far I know that vtable is a virtual table which has an array of pointers to virtual functions. Is there an article I can read with an example of a practical implementation? (Any walk through will be appreciated.)

like image 645
Simsons Avatar asked Aug 24 '10 09:08

Simsons


People also ask

What does the vtable function do?

The vtable contains an entry for each virtual function accessible by the class and stores a pointer to its definition. Only the most specific function definition callable by the class is stored in the vtable.

What is a vtable in programming?

In computer programming, a virtual method table (VMT), virtual function table, virtual call table, dispatch table, vtable, or vftable is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding).

What is vPointer and vtable?

vTable is a kind of function pointer array that contains the addresses all virtual functions of this class. Compiler builds this vTable at compile time. vPointer: Now for every object of a class that has a vTable associated with it, contains a vPointer in first 4 bytes. This vPointer points to the vTable of that class.

What is the type of vtable?

It has no type. It's an implementation detail unspecified by the standard; it is not part of the language. Note that C++ doesn't say that there has to be a virtual table or a virtual "pointer" at all (though this is the most common implementation of RTTI in C++ toolchains). Also, your analysis is wrong.


1 Answers

V-tables (or virtual tables) are how most C++ implementations do polymorphism. For each concrete implementation of a class, there is a table of function pointers to all the virtual methods. A pointer to this table (called the virtual table) exists as a data member in all the objects. When one calls a virtual method, we lookup the object's v-table and call the appropriate derived class method.

like image 78
doron Avatar answered Sep 22 '22 16:09

doron