So far, to allocate memory for objects, I've called the Create method. This is a constructor— a special method that you can apply to a class to allocate memory for an instance of that class. The instance is returned by the constructor and can be assigned to a variable for storing the object and using it later. All the data of the new instance is set to zero. If you want your instance data to start out with specific values, then you need to write a custom constructor to do that.
Use the constructor keyword in front of your constructor. Although you can use any name for a constructor, you should stick to the standard name, Create. If you use a name other than Create, the Create constructor of the base TObject class will still be available, but a programmer calling this default constructor might bypass the initialization code you've provided because they don't recognize the name.
By defining a Create constructor with some parameters, you replace the default definition with a new one and make its use compulsory. For example, after you define
type TDate = class public constructor Create (y, m, d: Integer);
you'll only be able to call this constructor and not the standard Create:
var ADay: TDate; begin // Error, does not compile: ADay := TDate.Create; // OK: ADay := TDate.Create (1, 1, 2000);
The rules for writing constructors for custom components are different, as you'll see in Chapter 9. The reason is that in this case you have to override a virtual constructor. Overloading is particularly relevant for constructors, because you can add multiple constructors to a class and call them all Create; this approach makes the constructors easy to remember and follows a standard path provided by other OOP languages in which constructors must all have the same name. As an example, I've added to the class two separate Create constructors: one with no parameters, which hides the default constructor; and one with initialization values. The constructor with no parameter uses as the default value today's date (as you can see in the complete code of the DataView example):
type TDate = class public constructor Create; overload; constructor Create (y, m, d: Integer); overload;
In the same way that a class can have a custom constructor, it can have a custom destructor—a method declared with the destructor keyword and called Destroy. Just as a constructor call allocates memory for the object, a destructor call frees the memory. Destructors are needed only for objects that acquire external resources in their constructors or during their lifetime. You can write custom code for a destructor, generally overriding the default Destroy destructor, to let an object execute some clean-up code before it is destroyed.
Destroy is a virtual destructor of the TObject class. You should never define a different destructor, because objects are usually destroyed by calling the Free method, and this method calls the Destroy virtual destructor of the specific class (virtual methods will be discussed later in this chapter).
Free is a method of the TObject class, inherited by all other classes. The Free method basically checks whether the current object (Self) is not nil before calling the Destroy virtual destructor. Free doesn't set the object to nil automatically; this is something you should do yourself! The object doesn't know which variables may be referring to it, so it has no way to set them all to nil.
Delphi 5 introduced a FreeAndNil procedure you can use to free an object and set its reference to nil at the same time. Call FreeAndNil(Obj1) instead of writing the following:
Obj1.Free; Obj1 := nil;
There's more on this topic in the section "Destroying Objects Only Once" later in this chapter.