Skip to main content

Class vtables

Suppose RECTANGLE is a class that extends from SHAPE, and our program creates a rectangle and assigns it to a variable whose type is SHAPE. We have lost the information that this object is really a rectangle, but several Hybrix language features need to recover this information somehow:

  • Virtual functions should invoke the HOOK of RECTANGLE instead of SHAPE.
  • The CLASS_ID() operator should return the ID of RECTANGLE instead of SHAPE.
  • The AS operator should give us the actual RECTANGLE variable, or NULL if the object is not actually a rectangle.

This magic relies on an hidden field called a vtable pointer that the compiler adds to the object's memory block. The term "vtable" is short for virtual method table, familiar from other programming languages such as C++.

The compiler creates a vtable for a Hybrix class...

  • ...if the class contains a HOOK function, or
  • ...if the class extends from the special CLASS_ID_BASE class, or
  • ...if the class extends from another class with a vtable.

Consequences of vtables

Certain restrictions apply to classes that have vtables:

  • They cannot be used with the LOCATED AT storage directive.
  • They cannot be used with the INSET storage directive.