Re: pointer syntax
- From: "Bruce Roberts" <dontsendtober@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 23 Nov 2005 09:46:58 -0500
"swansnow" <schultz@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1132687818.534993.261370@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> When you say "Pass by value" does that mean to use a "var" parameter?
> If you don't use a var parameter then all you're passing is a reference
> to the object? I understand the difference between pass-by-reference
> and pass-by-value, I'm just a bit confused about how Delphi does it.
NO.
Pass by Value: a copy of the value in the actual parameter is placed in the
formal parameter. IOW since the formal parameter is a copy, anything done
to it does not affect the actual parameter. In Delphi this is the default
passing mechanism.
Pass by Reference: the formal parameter is set to reference the actual
parameter. In terms of implementation one can think of the formal parameter
as being an automatically dereferenced pointer to the actual parameter. In
Delphi this passing mechanism is indicated by the presense of the VAR
keyword.
>
> I learned C way back when, then a bit of C++, but my most recent
> "other" language is Java. In Java, you have objects or primitives
> (int, char, and so forth are primitive). If you pass an object, you're
> really passing a reference (which is like an old-school pointer, you
> just can't dereference it). If you modify the object, then you'll see
> the changes persist. If you pass a primitive, you're actually creating
> a copy of the primitive's value and passing that.
>
> My understanding of Delphi follows. Please correct me where I'm wrong
> :)
>
> * Delphi makes a distinction in syntax between using objects and using
> non-objects like records
>
> * "normal" passing of non-objects is by value, and if you change them
> inside a procedure, the changes go away when the procedure exits.
>
> * If you say "var" in the argument list, then passing the non-object
> allows the procedure to make changes that persist. Kind of like having
> multiple return values.
>
> * If you have a non-object data structure, like a record, you can
> explicitly use pointers to avoid passing by value, and to allow
> modification of the structure inside a procedure (like old-school C
> programming)
>
> * 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.
>
> * If you have an object (an instance of a class), then you get stuff
> handled for you automatically, like in Java. You are passing references
> around, so changes persist. If you don't want changes to persist, then
> you have to explicitly create a copy (using a create statement, with
> perhaps a copy constructor) and work on that.
>
>
> It seems to me that I'm missing something...
>
> Question:
> If I have a TTable object, t, and I say:
>
> var
> t2 :TTable;
> begin
> t2 := t;
>
> ...what is the result? Do I now have two references pointing to the
> same object? If t's state is dsEdit, will t2's state be dsEdit, too?
>
> -Corinna
>
.
- References:
- pointer syntax
- From: swansnow
- Re: pointer syntax
- From: Maarten Wiltink
- Re: pointer syntax
- From: swansnow
- pointer syntax
- Prev by Date: Re: pointer syntax
- Next by Date: Re: Writing Binary Files with TFileStream
- Previous by thread: Re: pointer syntax
- Next by thread: Delphi Technical Reference Card
- Index(es):
Relevant Pages
|