Re: pointer syntax
- From: "Bjørge" <bjorge@xxxxxxxxxxxxxxx>
- Date: Tue, 22 Nov 2005 23:08:01 +0100
swansnow wrote:
> * If you have a non-object data structure, with "var" in the paramater
> list, you're passing by value, which means you're actually creating a
> copy of the data structure and passing that it. If you make changes to
> it, the changes get passed back, and so they persist. You can avoid
> using pointers (and their messy syntax) if you do things this way.
No, it's the opposite. 'var' means passing by reference. When used with a
pointer, it's the *pointer value* that's referenced, e.g., you could pass a
nil value in the object parameter, create an instance and assign this
instance to the object parameter - and have the reference once the routine
returns.
Example:
procedure CheckCreateStringList(var List: TStringList);
begin
if not Assigned(List) then
List:=TStringList.Create;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
List: TStringList;
begin
CheckCreateList(List);
List.Free;
end;
I guess someone would claim that this is strange coding style, but it's
perfectly valid ;-)
--
Bjørge
bjorge@xxxxxxxxxxxx
.
- Follow-Ups:
- Re: pointer syntax
- From: Maarten Wiltink
- Re: pointer syntax
- References:
- pointer syntax
- From: swansnow
- Re: pointer syntax
- From: Maarten Wiltink
- Re: pointer syntax
- From: swansnow
- pointer syntax
- Prev by Date: Re: Writing Binary Files with TFileStream
- Next by Date: Re: Writing Binary Files with TFileStream
- Previous by thread: Re: pointer syntax
- Next by thread: Re: pointer syntax
- Index(es):