Libraries - Object Constructor

A Constructor for a Object-Library is a method which is needed for creating an object. The constructor method is called by the compiler when an object is to be created/initialized. In object libraries at least one constructor for creating is necessary. The constructors for assignment to an object variable (ConstructorAssign) or copy (ConstructorClone), as well as the destructor are optional.

If a variable dimensioned by the type of the object library, it is undefined (nil). It is - as described - a pointer to a dynamic memoryblock. Once an object with the new-operator is created, this variable points to the allocated memory block.

How Much Memory is allocated for the object after creation, is defined by the author of the object library.

A constructor for creating (new) must therefore contain:

  1. If necessary, release of a already allocated memoryblock.
  2. Creating a memoryblock using the functions in the standard library in the desired size.
  3. Initialize the memoryblock with your data.
  4. Return of the address of the new memoryblock, or nil on error.

There are three different types of constructors in an object library that can be executed depending on the type of processing (creating, assigning, copying).

  1. Constructor - Called to create an temporary object: new myobject(…). The created object is then temporarily stored in memory and is not assigned to a variable. (more than one constructor allowed)
  2. ConstructorAssign - Called when an assignment: myobj1 = <Ausdruck>. This assigns a temporary created object to a variable.
  3. ConstructorClone - Wird aufgerufen wenn ein bestehendes Objekt instanziert (geklont/kopiert) werden soll: myobj1 = myobj2. Hierzu wird ein neues, temporäres Objekt erzeugt und die Daten aus dem Quellobjekt werden kopiert. Bei der Zuweisung von nil wird automatisch der Destructor aufgerufen.

The parameters that are defined for a constructor that control how and what kind of input data to generate an object are possible. Since multiple constructors may be applied, this may also differ.

Example 1

Constructor()
dim var as myobj
var = new myobject()

Example 2

Constructor(size as byte)
dim var as myobj
var = new myobject(123)
'following term selects and calls automatically the
'best-fit constructor with the value as parameter (like above):
var = 123