[fpc-pascal] Another Delphi mode question :) -- classes as parameter

Michael Van Canneyt michael at freepascal.org
Wed Jul 12 17:05:44 CEST 2006



On Wed, 12 Jul 2006, Alexandre Leclerc wrote:

> I tried the following:
>
> procedure ThisAndThat(bitmap: TBitmap);
> begin
> if not Assigned(bitmap) then
>   bitmap := TBitmap.Create;
> end;
>
> function Test: boolean;
> var
> bitmap: TBitmap;
> begin
> bitmap := nil;
> ThisAndThat(bitmap);
> Result := Assigned(bitmap);
> bitmap.Free;
> end;
>
> In Delphi a class is always treated as a 'var' when passed in a
> function since a class is a pointer to actual data; logical but
> confusing when begining in Delphi. So in Delphi Test() would return
> true.

Ahem.

Have you tried that ? Because it is manifestly NOT true:

-----------------------------------------------------------------------
procedure TForm2.DoTest(S : TStrings);

begin
   S:=TStringList.Create;
end;

procedure TForm2.WisaButton1Click(Sender: TObject);
Var
   S : TSTrings;

begin
   S:=Nil;
   DoTest(S);
   If S=Nil then
     ShowMessage('S still nil')
   else
     ShowMessage('S assigned');
end;
-----------------------------------------------------------------------

Shows 'S still nil'.

What is more, it gives a warning for the dotest method:

   [Hint] frmmain.pas(44): Value assigned to 'S' never used

What IS true is that you can change the properties of S, even if it is
passed by value or as const. But the pointer S cannot be changed.

So FPC is perfectly compliant.

Michael.



More information about the fpc-pascal mailing list