[fpc-pascal] various ways of passing a class instance as a parameter
Martin Schreiber
mse00000 at gmail.com
Tue Nov 15 11:24:09 CET 2011
On Tuesday 15 November 2011 11.00:34 Graeme Geldenhuys wrote:
> >> procedure foo(const AClass: TStringList);
> >
> > Take a copy of the AClass instance pointer, AClass is readonly.
>
> This one confused me a bit. I thought the whole object would be
> read-only, but in fact it is just the AClass instance pointer which
> cannot be modified. The properties of AClass are still read-write.
The wording was bad. Should be:
"
> procedure foo(AClass: TStringList);
> or
Take a copy of the AClass instance pointer.
> procedure foo(var AClass: TStringList);
Take the address of the instance variable. The instance pointer pointed by the
address can be changed by the procedure, so the type of the instance variable
must match TStringList exactly otherwise the procedure could store a wrong
class into the instance variable. Example:
procedure foo(var AClass: TList);
begin
aclass.free;
aclass:= Tlist.create;
end;
[...]
var
cl1: TStringList.
begin
foo(cl1); //does not compile
//now there would be a TList in a TstringList variable
> or
> procedure foo(const AClass: TStringList);
>
Take a copy of the AClass instance pointer, AClass instance pointer is
readonly.
> or
> procedure foo(constref AClass: TStringList);
Take the address of the instance variable, AClass instance pointer pointed by
the address is readonly.
"
Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20111115/2e527c35/attachment.html>
More information about the fpc-pascal
mailing list