ArticleslgStudy

computer science

Virtual method table

Virtual method table is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Virtual method table rather than just read about it. In short: 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). Whenever a class defines a virtual function (or method), most compilers add a hidden member variable to the class that points to an array of pointers to (virtual) functions called t…

Key takeaways

  • Virtual method table belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Virtual method table to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Virtual method table from memory before moving on to harder problems.

Reference excerpt

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). Whenever a class defines a virtual function (or method), most compilers add a hidden member variable to the class that points to an array of pointers to (virtual) functions called the virtual method table. These pointers are used at runtime to invoke the appropriate function implementations, because at compile time it may not yet be known if the base function is to be called or a derived one implemented by a class that inherits from the base class. There are many different ways to implement such dynamic dispatch, but use of virtual method tables is especially common among C++ and related languages (such as D and C#). Languages that separate the programmatic interface of objects from the implementation, like Visual Basic and Delphi, also tend to use this approach, because it allows objects to use a different implementation simply by using a different set of method pointers. The method allows creation of external libraries, where other techniques perhaps may not. Suppose a program contains three 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 function on a Cat reference (which can refer to an instance of Cat, or an instance of HouseCat or Lion), the code must be able to determine which implementation of the function the call should be dispatched to. This depends on the actual class of the object, not the class of the reference to it (Cat). The class cannot generally be determined statically (that is, at compile time), so neither can the compiler decide which function to call at that time. The call must be dispatched to the right function dynamically (that is, at run time) instead.

Implementation An object's virtual method table will contain the addresses of the object's dynamically bound methods. Method calls are performed by fetching the method's address from the object's virtual method table. The virtual method table is the same for all objects belonging to the same class, and is therefore typically shared between them. Objects belonging to type-compatible classes (for example siblings in an inheritance hierarchy) will have virtual method tables with the same layout: the address of a given method will appear at the same offset for all type-compatible classes. Thus, fetching the method's address from a given offset into a virtual method table will get the method corresponding to the object's actual class. The C++ standards do not mandate exactly how dynamic dispatch must be implemented, but compilers generally use minor variations on the same basic model. Typically, the compiler creates a separate virtual method table for each class. When an object is created, a pointer to this table, called the virtual table pointer, vpointer or VPTR, is added as a hidden member of this object. As such, the compiler must also generate "hidden" code in the constructors of each class to initialize a new object's virtual table pointer to the address of its class's virtual method table. Many compilers place the virtual table pointer as the last member of the object; other compilers place it as the first; portable source code works either way. For example, g++ previously placed the pointer at the end of the object.

Example Consider the following class declarations in C++:

g++ 3.4.6 from GCC produces the following 32-bit memory layout for the object base2:

b2: +0: pointer to virtual method table of Base2 +4: value of b2

virtual method table of Base2: +0: Base2::fn2()

and the following memory layout for the object derived:

derived: +0: pointer to virtual method table of Derived (for Base1) +4: value of b1 +8: pointer to virtual method table of Derived (for Base2) +12: value of b2 +16: value of d

Total size: 20 Bytes.

virtual method table of Derived (for Base1): +0: Base1::fn1() // Base1::fn1() is not overridden

virtual method table of Derived (for Base2): +0: Derived::fn2() // Base2::fn2() is overridden by Derived::fn2()

Note that those functions not carrying the keyword virtual in their declaration (such as nonVirtual() and d()) do not generally appear in the virtual method table. There are exceptions for special cases as posed by the default constructor. Also note the virtual destructors in the base classes, Base1 and Base2. They are necessary to ensure delete derived; can free up memory not just for Derived, but also for Base1 and Base2, if derived is a pointer or reference to the types Base1 or B2. They were excluded from the memory layouts to keep the example simple. Overriding of the method fn2() in class Derived is implemented by duplicating the virtual method table of Base2 and replacing the pointer to Base2::fn2() with a pointer to Derived::fn2().

Multiple inheritance and thunks The g++ compiler implements the multiple inheritance of the classes Base1 and Base2 in class Derived using two virtual method tables, one for each base class. (There are other ways to implement multiple inheritance, but this is the most common.) This leads to the necessity for "pointer fixups", also called thunks, when casting. Consider the following C++ code:

While derived and base1 will point to the same memory location after execution of this code, base2 will point to the location derived + 8 (eight bytes beyond the memory location of derived). Thus, base2 points to the region within derived that "looks like" an instance of Base2, i.e., has the same memory layout as an instance of Base2.

Invocation A call to derived->fn1() is handled by dereferencing derived's Derived::Base1 vpointer, looking up the fn1 entry in the virtual method table, and then dereferencing that pointer to call the code.

Single inheritance In the case of single inheritance (or in a language with only single inheritance), if the vpointer is always the first element in derived (as it is with many compilers), this reduces to the following pseudo-C++:

Where *derived refers to the virtual method table of Derived and [0] refers to the first method in the virtual method table. The parameter derived becomes the "this" pointer to the object.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Virtual method table

Start with the simplest possible case. Write down what Virtual method table claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Virtual method table before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Virtual method table ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Virtual method table

In research
Virtual method table appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Virtual method table in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Virtual method table is common in secondary-school and first-year university syllabi. It links to neighbouring topics Method (computer programming), so understanding it makes those chapters shorter.
In everyday life
Look for Virtual method table outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Virtual method table” →

How to study Virtual method table in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Virtual method table means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Virtual method table out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Virtual method table in simple terms?

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). Whenever a class defines a virtual function (or method), most…

Why does Virtual method table matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Virtual method table?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Virtual method table.

Tags

  • Method (computer programming)

Keep exploring