Interface Dispatch (2018)

Interface Dispatch (2018)

Virtual method calls are simple: you just look up the method slot in a vtable and call the function pointer. In short, one of the important features of OOP is that a virtual method call doesn’t depend on the static type of a variable, but on the dynamic type of the object. If we desugar the above C++ code to C, the method call would actually look like this:

Equivalently to a struct of function pointers we can think of an array of function pointers slots, where we have to know the slot index at compile time:

The object then points to a vtable that is filled with the correct method implementations for its dynamic type.

Source: lukasatkinson.de