14
votes

I have a doubt that I cannot solve. I have read the documentation at embarcadero for class constructors but I cannot understand the meaning of that. In other words, what is the usage difference between a constructor and a class constructor? I did this:

type
 TGeneric<T> = class
  private
   FValue: T;
   aboutString: string;
   procedure setValue(const a: T);
  public
   property V: T read FValue write setValue;
   property about: string read aboutString;
   constructor Create;
   destructor Destroy; override;
 end;

implementation

{ TGeneric<T> }

constructor TGeneric<T>.Create;
begin
  inherited;
  aboutString := 'test';
end;

Instead this code is not working properly:

type
 TGeneric<T> = class
  private
   FValue: T;
   aboutString: string;
   procedure setValue(const a: T);
  public
   property V: T read FValue write setValue;
   property about: string read aboutString;
   class constructor Create;
   destructor Destroy; override;
 end;

implementation

{ TGeneric<T> }

class constructor TGeneric<T>.Create;
begin
  inherited;
  aboutString := 'test';
end;

I guess that the answer is in this line of the documentation:

Normally, class constructors are used to initialize the static fields of the class or to perform a type of initialization, which is required before the class or any class instance can function properly.

Tell me if I am correct:

  • Constructor: I can use the inherited Create;, initialize variables and so on.
  • Class constructor: I can use this when I have to create an object in my class immediately?

For example look at below:

type
   TBox = class
   private
     class var FList: TList<Integer>;
     class constructor Create;
   end;

 implementation

 class constructor TBox.Create;
 begin
   { Initialize the static FList member }
   FList := TList<Integer>.Create();
 end;

 end.

Here I am going to create the object immediately when I call TBox.Create in the main form?

1
Think of class constructors as a way to automatically initiate your class type. They are automatically called at startup and you should never call them by your code. In other words, class constructors operates on the type, while normal constructors operates on your variables.LU RD
So I can avoid that and only care about the classic constructor?Raffaele Rossi
That depends on what you want to do. A class var is a sort of global variable for that type and can be useful in some cases.LU RD

1 Answers

24
votes
  • A class constructor executes exactly once, when the unit in which it is declared is initialized. A class constructor is a static class method, and so Self is not defined.
  • A constructor executes when explicitly called and has the job of initializing an instance of a class.

In the wild, class constructors are rare, constructors are as common as muck. Quite likely you have no immediate need for class constructors so feel free to ignore them for now. Concentrate on understanding how to write and use constructors.

If in the future you ever need to initialize variables owned by the class (as opposed to owned by an instance) then you might find yourself wanting to use a class constructor. Until that point in time, do feel free to ignore them.