When this is run -
program test1;
{$mode objfpc}
type
TA = class
constructor create;
function WhoAmI: string; virtual;
end;
TB = class(TA)
constructor create;
function WhoAmI: string; override;
end;
TC = class of TA;
constructor TA.Create;
begin
WriteLn('constructor A and I am ', WhoAmI);
end;
function TA.WhoAmI: string;
begin
Result:= 'TA';
end;
constructor TB.Create;
begin
WriteLn('constructor B and I am', WhoAmI);
inherited Create;
end;
function TB.WhoAmI: string;
begin
Result:= 'TB';
end;
function DoCreate(aClass: TC): TA;
begin
Result:=aClass.Create;
end;
var
Z: TA;
begin
Z:= DoCreate(TB);
end.
The output is -
constructor A and I am TB
Can anyone please explain why TB.constructor is not called.
Changing the last 4 lines to
var
Z: TB;
begin
Z:= TB(DoCreate(TB));
end.
produces the same output.