On 2011-11-24 03:58, Luiz Americo Pereira Camara wrote:
> possible to get the expected behavior without forcing programmer to
> create a virtual constructor by using the new RTTI

What has the "new RTTI" got to do with anything?

Simply define TObj.Create as virtual, and TFoo.Create as overridden.
This is rather normal coding practise with classes. Nothing special.


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

  TFoo = class(TObj)
  public
    constructor create; override;
  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.
----8<-------------8<-------------8<-------------8<-------------8<----


And here is the output...

$ ./test
cls class is TFoo
TObj.create
TFoo.Create



Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to