[next] [prev] [prev-tail] [tail] [up]

### 6.2 Class instantiation

Classes must be created using one of their constructors (there can be multiple constructors). Remember that a class is a pointer to an object on the heap. When a variable of some class is declared, the compiler just allocates room for this pointer, not the entire object. The constructor of a class returns a pointer to an initialized instance of the object on the heap. So, to initialize an instance of some class, one would do the following :

ClassVar := ClassType.ConstructorName;

The extended syntax of new and dispose can not be used to instantiate and destroy class instances. That construct is reserved for use with objects only. Calling the constructor will provoke a call to getmem, to allocate enough space to hold the class instance data. After that, the constuctor’s code is executed. The constructor has a pointer to its data, in Self.

Remark:

- The {$PackRecords } directive also affects classes, i.e. the alignment in memory of the different fields depends on the value of the {$PackRecords } directive.
- Just as for objects and records, a packed class can be declared. This has the same effect as on an object, or record, namely that the elements are aligned on 1-byte boundaries, i.e. as close as possible.
- SizeOf(class) will return the same as SizeOf(Pointer), since a class is a pointer to an object. To get the size of the class instance data, use the TObject.InstanceSize method.

[next] [prev] [prev-tail] [front] [up]