[fpc-devel] Hom to initialize an object (old style object, not TObject)

Tomas Hajny XHajT03 at hajny.biz
Fri Nov 1 16:10:25 CET 2013


On Fri, November 1, 2013 15:59, Martin wrote:
> If I have the code as shown at the end of the mail, i get
>
> project1.lpr(6,9) Warning: Function result variable does not seem to
> initialized
>
> but how can I initialize it?
>
> **** Unit1
> unit Unit1;  {$mode objfpc}{$H+}
> interface
> uses  Classes, SysUtils;
> type
>    TBar = object
>    private
>      a: integer;
>    public
>      procedure Init;
>    end;
>
> implementation
> procedure TBar.Init;
> begin
>    a := 0;
> end;
> end.
>
>
> **** main program
>
> program Project1;
> uses Unit1;
>
> function Foo: TBar;
> begin
>    Result.Init;
> end;
>
> begin
>    Foo;
> end.

Sorry if I overlooked something, but where is the variable of that type
declared in your code? These objects may be either declared statically
("var B: TBar") or dynamically ("Result := New (TBar.Init)" - but then the
return value must be of type PBar, not TBar). If I understand it
correctly, you initialize the object on the stack, but it would disappear
from there (gets overwritten) immediately after the function return (the
following function call would overwrite it).

Tomas





More information about the fpc-devel mailing list