Wednesday, December 11, 2013

vtable : virtual method table

Words from wikipedia about virtual method table: 

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

Suppose a program contains several classes in an inheritance hierarchy: a superclass, Cat, and two subclasses, HouseCat and Lion. Class Cat defines a virtual function named speak, so its subclasses may provide an appropriate implementation (e.g. either meow or roar).

When the program calls the speak method on a Cat pointer (which can point to a Cat class, or any subclass of Cat), the calling code must be able to determine which implementation to call, depending on the actual type of object that is pointed to. Because the type of object pointed to by the Cat pointer is not determined at compile-time, the decision as to which branch to take cannot be decided at compile-time.

There are a variety of different ways to implement such dynamic dispatch, but the vtable (virtual table) solution is especially common among C++ and related languages (such as D and C#).

Here is a graphic description about what this means.



No comments:

Post a Comment