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#). Languages which separate the programmatic interface of objects from the implementation, like Visual Basic and Delphi, also tend to use the vtable approach, because it allows objects to use a different implementation simply by using a different set of method pointers.
Read more about Virtual Method Table: Implementation, Example, Multiple Inheritance and Thunks, Invocation, Efficiency, Comparison With Alternatives
Famous quotes containing the words virtual, method and/or table:
“Tragedy dramatizes human life as potentiality and fulfillment. Its virtual future, or Destiny, is therefore quite different from that created in comedy. Comic Destiny is Fortunewhat the world will bring, and the man will take or miss, encounter or escape; tragic Destiny is what the man brings, and the world will demand of him. That is his Fate.”
—Susanne K. Langer (18951985)
“I have a new method of poetry. All you got to do is look over your notebooks ... or lay down on a couch, and think of anything that comes into your head, especially the miseries.... Then arrange in lines of two, three or four words each, dont bother about sentences, in sections of two, three or four lines each.”
—Allen Ginsberg (b. 1926)
“When you got to the table you couldnt go right to eating, but you had to wait for the widow to tuck down her head and grumble a little over the victuals, though there warnt really anything the matter with them. That is, nothing only everything was cooked by itself. In a barrel of odds and ends it is different; things get mixed up, and the juice kind of swaps around, and the things go better.”
—Mark Twain [Samuel Langhorne Clemens] (18351910)