Re: pointer syntax
- From: "swansnow" <schultz@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: 22 Nov 2005 11:30:18 -0800
Maarten said:
>If so, just pass the object by value, and call the method on it.
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.
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
.
- Follow-Ups:
- Re: pointer syntax
- From: Bruce Roberts
- Re: pointer syntax
- From: Maarten Wiltink
- Re: pointer syntax
- From: Bjørge
- Re: pointer syntax
- References:
- pointer syntax
- From: swansnow
- Re: pointer syntax
- From: Maarten Wiltink
- 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):
Relevant Pages
|