Constructors and destructors
Sometimes you want to initialize the field of a struct or execute some action right after creating an instance of it. Spice enables you doing that, by supporting constructors. To clean up a struct instance, destructors can be used.
Constructors¶
Whenever you create a struct instance like this, Spice will automatically call the ctor
method of this struct:
Constructors can also have arguments to pass some information from the caller to the constructor of the struct:
Spice | |
---|---|
The ctor
method can also be called manually like calling other methods.
Destructors¶
You have the option to create a destructor by providing a dtor
method on a struct. It does not allow any arguments and has no return type, since it is a procedure. Destructors can be especially useful for de-allocating objects in heap memory, that were allocated via malloc()
. Whenever a struct variable goes out of scope somewhere in the program, the compiler searches for a destructor and calls it if available.
Here is an example for a custom destructor: