Re: Pointer vs Reference



On 2007-09-30 07:16, Roedy Green wrote:
On Sat, 29 Sep 2007 22:56:52 GMT, Erik Wikström
<Erik-wikstrom@xxxxxxxxx> wrote, quoted or indirectly quoted someone
who said :

Foo f1 = new Foo();
Foo f2 = f1;

While it does not exactly take the address of the object referred to by
f1, it does create a new reference (pointer) that points to the same object.

In assembler, what that does in allocate space for a new Foo object on
the heap somewhere. It puts the 32-bit address of that object in f1, a
local variable allocated on the stack frame.

Then it copies the pointer value from f1 and stores it in f2.

No reference is created, except on the initial allocation.

Removing comp.lang.c++ form the discussion since it is becoming Java only.

Oh, I see, I was under the impression that f1 was a reference. Just out
of curiosity what is a reference in Java? Does f1 (and later f2) point
to a reference which refers to the Foo object, or is the Foo object the
reference (which would be quite counter intuitive)?

--
Erik Wikström
.



Relevant Pages

  • Re: 7.0 wishlist?
    ... The "auto-implement" is intended mainly for the odd situation where an existing class you can't edit fits some interface and you're willing to take responsibility for it if it turns out not to actually adhere to the contract, and try using it where that interface type is expected. ... If reference declarations started showing up with the odd asterisk, bang, or other punctuation mark on it, but never primitive declarations, people would probably be able to guess what was going on, on the basis of "what other binary flag might be set on references but not primitives and would be really useful besides can be/cannot be null?" ... the compiler cannot prove by static analysis that the RHS isn't null might be a good idea. ... Object foo, bar; ...
    (comp.lang.java.programmer)
  • Re: 7.0 wishlist?
    ... This is a slight modifier that would be very common on reference declarations, so it would be good to economize on typing or visual clutter where it was used. ... which has the enum constants actually singleton instances of same-named subclasses. ... enum Foo ... The point being to allow to publish an interface ...
    (comp.lang.java.programmer)
  • Re: C++ design question
    ... >>void DoStuffWithBarBase ... I assumed that the Foo subclasses each had specialized responsibilities, ... policies for object and relationship instantiation (object ... conditional one can't implement it with a reference. ...
    (comp.object)
  • Re: How to undo a bless on a GLOB
    ... The reference was blessed to something: ... bless $foo, 'Something'; ... Can you bless the glob before ... That could be a bug in POE. ...
    (comp.lang.perl.misc)
  • Re: pass by Reference/value ???
    ... > void foo ... this would be called "passing by reference". ... foo receives a local copy of s and any changes that it makes to s only ... Since s is a pointer, a variable storing the address of s is a pointer to ...
    (comp.lang.cpp)