Re: pointer syntax



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

.



Relevant Pages

  • Re: pointer syntax
    ... > really passing a reference (which is like an old-school pointer, ... > a copy of the primitive's value and passing that. ... build them yourself with pointers. ... mean a var parameter, write a var parameter. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Passing by reference
    ... It depends upon the reason one is passing the reference. ... instantiate all relationships via a constructor so there will always be ... I naturally think of 'getter' methods as 'knowledge ...
    (comp.object)
  • Re: Using ref
    ... the address of a variable that contains a reference to an object, ... the C version is passing the address of a variable that contains the   ... Inasmuch as the specification is unambiguous, there is a single truth. ... Every time he states the incorrect view, I will feel compelled to correct his incorrect statements. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Derived type argument to subroutine
    ... In practice, they often, but not always are. ... I can rely on the fact that arguments are passed by reference. ... Would the printed value always be 4, or is it undefined as the passing ... With an important exception, ...
    (comp.lang.fortran)
  • Re: Passing an object reference 1000 times
    ... Nicholas Paldino ... i do know i am passing a *reference* to the object; ... >> I think that the assumption you are making is that when you are>> passing a reference type around, you are copying it as you make the call ...
    (microsoft.public.dotnet.languages.csharp)