[fpc-pascal] Class reference doubt

Jesus Reyes jesusrmx at yahoo.com.mx
Wed Nov 23 22:48:20 CET 2011


in the following example The output is:
cls class is TFoo
TObj.create

where I would expect:
cls class is TFoo
TObj.create
TFoo.create

ie the TFoo.constructor is not called, is this normal/expected?, The documentation does clarify the situation: http://www.freepascal.org/docs-html/ref/refse31.html 

"Class reference types are used to create instances of a certain class, which is not yet known at compile time, but which is specified at run time. Essentially, a variable of a class reference type contains a pointer to the definition of the speficied class. This can be used to construct an instance of the class corresponding to the definition, or to check inheritance." 

Thanks.

Jesus Reyes A.

program test;
{$mode ObjFpc}{$H+}
type
  TObj = class
  public
    constructor create;
  end;
  TObjClass=class of TObj;

  TFoo = class(TObj)
  public
    constructor create;
  end;

constructor TObj.Create;
begin
  inherited create;
  WriteLn('TObj.create');
end;

constructor TFoo.create;
begin
  inherited Create;
  WriteLn('TFoo.Create');
end;

var
  cls: TObjClass;
  obj: TObj;
begin
  cls := TFoo;
  WriteLn('cls class is ',cls.ClassName);
  Obj := cls.Create;
  Obj.Free;
end.




More information about the fpc-pascal mailing list