[fpc-pascal] various ways of passing a class instance as a parameter

2011-11-15 Thread Graeme Geldenhuys
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
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] various ways of passing a class instance as a parameter

2011-11-15 Thread Alexander Shishkin

15.11.2011 13:33, Graeme Geldenhuys пишет:

Hi,

What exactly is the difference (if any) between the parameter modifier
when you pass a class instance to a procedure?



I your example there is no difference, except that var could be ~0.01% 
slower


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] various ways of passing a class instance as a parameter

2011-11-15 Thread michael . vancanneyt



On Tue, 15 Nov 2011, Graeme Geldenhuys wrote:


Hi,

What exactly is the difference (if any) between the parameter modifier
when you pass a class instance to a procedure?


It behaves exactly the same as if you would pass a typed pointer.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] various ways of passing a class instance as a parameter

2011-11-15 Thread Martin Schreiber
On Tuesday 15 November 2011 10.33:13 Graeme Geldenhuys wrote:
 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

Take a copy of the AClass instance pointer.

 procedure foo(var AClass: TStringList);

Take the address of the instance variable. The instance pointer 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 is readonly.

   or
 procedure foo(constref AClass: TStringList);

Take the address of the instance variable, AClass is readonly.

Martin
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] various ways of passing a class instance as a parameter

2011-11-15 Thread Graeme Geldenhuys
On 15/11/2011, Martin Schreiber mse0@g. wrote:


Thanks Martin. Extending my example by changing the body of foo() too...

  AClass.Free;
  AClass := TStringList.Create;
  AClass.Add('inside foo');

...reveals a bit more about the differences.



 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.


-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://fpgui.sourceforge.net
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] various ways of passing a class instance as a parameter

2011-11-15 Thread Martin Schreiber
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
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal