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 of
RECTANGLEinstead ofSHAPE. - The AS operator should give us the actual
RECTANGLEvariable, 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_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.