[fpc-pascal] Class reference doubt
Graeme Geldenhuys
graemeg.lists at gmail.com
Thu Nov 24 09:47:03 CET 2011
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/
More information about the fpc-pascal
mailing list