When you declare a new class, it’s always a good idea to include a call to the inherited constructor, like this:
Listing 1
Calling an inherited constructor
constructor TItem.Create;
begin
inherited;
end;
If you forget to call the inherited constructor, then any fields it would have initialized will remain at their default values. This may lead to access violations, invalid pointer operations, or other unexpected behavior later in your program.
If the signature of the class’s constructor is not the same as
that of the parent class’s, then the inherited call also needs to include the name of
the constructor, plus any arguments it requires, as demonstrated below.
This is true regardless of whether the base or descendant constructors
are virtual.
Listing 2
An inherited constructor call with different parameters
type
TBaseItem = class
public
constructor Create(const AMessage: string);
end;
TDescendantItem = class(TBaseItem)
private
FID: Cardinal;
public
constructor Create(const AID: Cardinal);
end;
constructor TDescendantItem.Create(const AID: Cardinal);
begin
inherited Create('default value');
FID := AID;
end;