Re: pointer syntax



"swansnow" <schultz@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1132351602.308119.6450@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

[...]
> In the following code snippet, I would like [the procedure] to be
> able to modify [the parameter object] by calling [a method on it],
> and having [the object] stay changed when the procedure returns.

Is that a fair modification of the original text? If so, just pass
the object by value, and call the method on it. You're allowed to
do that. It doesn't change the value of the _parameter_, which is a
reference to an object. It may change the values stored inside the
object, but that's considered irrelevant.


> while (not query1.eof) do
> begin
> processRecord(query1); { <--- I should use @query1
> as a parameter, right?}

No. Definitely not. This is how you pass parameters by reference in
C. In Pascal, you simply make it a var parameter in the receiving
procedure declaration and it works.

The mechanism used to pass variables by reference and use them inside
the procedure is the same in C and in Pascal, but C exposes how it's
done while Pascal leaves that invisible. Different language
philosophies.


> prn.IncBar(1);
> query1.next;
> end;
>
> What does the declaration of processRecord look like?
> procedure TmyClass.processRecord( q : TQuery);
> procedure TmyClass.processRecord(q^ : TQuery);

If you _have_ to do something like that (and sometimes you do), you
change not the variable/parameter but its type.

procedure ProcessRecords(const Records: ^TQuery);

or declare a named type for it and use that:

type PQuery = ^TQuery;
procedure ProcessRecords(const Records: PQuery);

Replacing the T prefix on a type identifier with a P for pointer types
is a convention. The compiler doesn't care, people who have to read it
will.

But don't do this unless you _know_ you have to. Use a var parameter
if you want to persistently change its (the parameter's) value inside
the procedure, and usually pass objects by value because you don't need
to change the object reference to use the object anyway.

Groetjes,
Maarten Wiltink


.