DEFSTRUCT
General Form:
DEFSTRUCT can be considered similar to a DEFRECORD but the component fields are individually initialised when it is instantiated. However it acts more like a class in that it has inheritance through the EXTENDS clause. A base struct can be subclassed by defining the new struct with a EXTENDS STRUCT referencing the base struct. This new struct will have all the fields of the base struct the new fields.
This class-like behaviour also includes class methods which can be declared as FLDs and which, when invoked will always pass the structure BYREF as an extra first argument.
DEFSTRUCT c FLD n AS Integer FLD x$ AS INIT PTR FLD m = 'myFn END STRUCT DEFSUB 'myFn(BYREF this$_c, arg$) REDIM FLD(this$.x$) = arg$ END SUB LOCAL DIM c$_c, c c$.m("Hello") c = NEW _c SYM(*c)$.m("World")
When a structure is extended the base methods can be redefined.
DEFSTRUCT like DEFRECORD supports a local scoped variant LOCAL DEFSTRUCT.
A DEFSTRUCT will occupy more memory than a DEFRECORD with the same FLDs because of the class overhead.
Constructor / Destructor
DEFSTRUCT also has two special methods which are automatically invoked on creation and deletion of the variable.
$COMPLIANCE 3 DEFSTRUCT c FLD Constructor = 'myConstructor FLD Destructor = 'myDestructor END STRUCT DEFSUB 'myConstructor(BYREF this$_c) PRINT "Constructor" END SUB DEFSUB 'myDestructor(BYREF this$_c) PRINT "Destructor" END SUB 'main() $END DEFSUB 'main() LOCAL DIM c$_c PRINT "*****" ENDSUBOutput
Constructor ***** Destructor
Compatibility
DEF STRUCT was introduced with KCML 6.90.
See also: