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
hookofrectangleinstead ofshape. - The
class_id()operator should return the ID ofrectangleinstead ofshape. - The
asoperator should give us the actualrectanglevariable, ornullif 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
hookfunction, or - ...if the class extends from the special
class_id_baseclass, 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 atstorage directive. - They cannot be used with the
insetstorage directive.