Virtual Tables Essay

Submitted By guilder10
Words: 370
Pages: 2

Virtual Tables:- The virtual table is a lookup table of functions used to resolve function calls in a dynamic or late binding manner. The virtual table is also called by other names, such as vtable or virtual function table, virtual method table or dispatch table. Every class that uses virtual functions or which is derived from a class that uses virtual functions is given its own virtual table. The vtable is a static array that the compiler sets up at compile time. A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this table is a function pointer that points to the most-derived function accessible by the class. The compiler also adds a hidden pointer to the base class, which will be *__vptr. *__vptr which is set automatically when a class instance is created so that it points to the virtual table for that class. The *this pointer, which is actually a function parameter used by the compiler to resolve self-references, *__vptr is a real pointer. Consequently, it makes each class object allocated bigger by the size of one pointer. It also means that *__vptr is inherited by derived classes. Virtual functions are most commonly implemented using virtual function tables, or vtables. Each class that declares at least one virtual function has a hidden data member (e.g., _ _vtbl). The _ _vtbl member points to an array of function pointers. Every derived class has a copy of the table. Every instance of a class…