Next: 5.3 Methods
Up: 5. Classes
Previous: 5.1 Class definitions
Classes must be created using their constructor. Remember that a class is a
pointer to an object, so when you declare a variable of some class, the
compiler just allocates a pointer, not the entire object. The constructor of
a class returns a pointer to an initialized instance of the object.
So, to initialize an instance of some class, you would do the following :
ClassVar := ClassType.ConstructorName;
You cannot use the extended syntax of new and dispose 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 it's 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, you can declare a packed class.
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 4, since a class is but a pointer to
an object. To get the size of the class instance data, use the
TObject.InstanceSize method.
root
1999-06-10