[fpc-devel]Proposal for audo-destructors

Kalabukhov Alex kalab at archit.vrn.ru
Sun Sep 12 15:05:51 CEST 2004


Do you need second C++ with pascal syntax? :)

Auto-destructors? It's very simple!!! :)

(*====================================================*)

unit uGC;

interface

type
  IAutoObject = interface
    function GetObject: TObject;
    property AObject: TObject read GetObject;
  end;

  TAutoObject = class(TInterfacedObject, IAutoObject)
  private
    FObj: TObject;
  public
    constructor Create(Object_: TObject); virtual;
    destructor Destroy; override;
    function GetObject: TObject;
  end;

  function IObject(Object_: TObject): IAutoObject;

implementation

constructor TAutoObject.Create(Object_: TObject);
begin
  FObj := Object_;
end;

destructor TAutoObject.Destroy;
begin
  if FObj <> nil then
    FObj.Free;
  inherited;
end;

function TAutoObject.GetObject: TObject;
begin
  Result := FObj;
end;

function IObject(Object_: TObject): IAutoObject;
begin
  Result := TAutoObject.Create(Object_);
end;

end.

(*====================================================*)

Example:

var
  Tmp: TMyClass;
begin
  Tmp := IObject(TMyClass.Create).AObject as TMyObject;
end;


>Absence of automatic construction/destruction of 
>classes is very painfull and it can significantly 
>degrade the
>development process (i.e. smart pointers and RAII are 
>impossible). It's one of the greatest flaws of Pascal 
>relatevly to
>C   (the second thing are Templates of cource). Are 
>there any plans for serious language extensions in 
>this way for "post
>2.0" releases of FPC ?






More information about the fpc-devel mailing list