A bit of a convoluted question, I know, and I'm sure someone is at hand to simplify it to its basics.
Consider the following code:
TTestClass = class
public
end;
TTestClassDescendant = class(TTestClass)
public
constructor Create;
end;
implementation
procedure TForm1.Button1Click(Sender: TObject);
var tc: TTestClass;
begin
tc := TTestClassDescendant.Create;
tc.Free;
end;
{ TTestClassDescendant }
constructor TTestClassDescendant.Create;
begin
ShowMessage('Create executed') // this gets executed
end;
The Create procedure gets executed properly.
Now consider following code:
TTestClass = class
public
end;
TTestClassDescendant = class(TTestClass)
public
constructor Create;
end;
TTestClassClass = class of TTestClass;
implementation
procedure TForm1.Button1Click(Sender: TObject);
var tc: TTestClass;
tcc: TTestClassClass;
begin
tcc := TTestClassDescendant;
tc := tcc.Create;
tc.Free
end;
{ TTestClassDescendant }
constructor TTestClassDescendant.Create;
begin
ShowMessage('Create executed') // this does NOT get executed
end;
The Create procedure of the descendant class does not get executed anymore.
However, if I introduce a constructor in the parent class and override it in the descendant class, it does get executed:
TTestClass = class
public
constructor Create; virtual;
end;
TTestClassDescendant = class(TTestClass)
public
constructor Create; override;
end;
Pardon me if I'm overlooking the obvious, but shouldn't the constructor code in that second block of code be executed when the construction occurs through a class variable, just as it is when it is called through the class identifier itself?