[fpc-pascal] various ways of passing a class instance as a parameter
Graeme Geldenhuys
graemeg.lists at gmail.com
Tue Nov 15 10:33:13 CET 2011
Hi,
What exactly is the difference (if any) between the parameter modifier
when you pass a class instance to a procedure?
In the example below, I can define foo() as follows...
procedure foo(AClass: TStringList);
or
procedure foo(var AClass: TStringList);
or
procedure foo(const AClass: TStringList);
...and the program output is always the same. As in the first case
where I don't specify var or const, how does FPC treat the AClass
parameter?
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
program a;
{$mode objfpc}{$H+}
uses
Classes;
procedure foo(AClass: TStringList);
begin
AClass.Add('inside foo');
end;
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Add('inside main');
foo(sl);
sl.Add('the end');
writeln(sl.Text);
finally
sl.free;
end;
end.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Here is the program output:
$ ./a
inside main
inside foo
the end
--
Regards,
- Graeme -
_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://fpgui.sourceforge.net
More information about the fpc-pascal
mailing list