Re: Pointer vs Reference
Summercool wrote:
Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":
Pointer and Reference
---------------------
I am starting to see what pointer and reference are and how they
relate
to each other.
in the C era, a pointer *is* a reference. that's why when we have
int a = 10;
int *pi = &a;
and you can "dereference it":
*pi = 20;
Until when C++ comes along, then we have a new "reference":
int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.
int i = &a; is the syntax. Yes, the ampersand is used for both reference
and address-of, so it can be confusing (especially since I think that i
would be set to the address of a in C).
i = 20; // now both a and i are 20
so this type of reference is an implicit pointer... it points to a,
I wouldn't call it an implicit pointer: you cannot, to my knowledge,
change i to point to any other object but a like you can with a pointer.
Rather, i is an alias of a: the two objects forever more point to the
same thing.
so a reference is new: a pointer but "looks like not a pointer".
Not quite. See above.
come to think about it, in Java and Ruby, they are like that too.
No, it is not. In syntax, Java more nearly follows the reference syntax
but it is closer to a pointer with transparent {de}referencing.
In language we use nowadays, which language has "reference"
In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.
In language we use nowadays, which language has "reference" to a
"reference"?
I think C++, Java, PHP5 do... not sure about Python and Ruby.
Let me start by asking a question to the C++ language lawyers out there:
what should this print out:
int a = 50, b = 20;
int i = &a;
std::cout << "a: " << a << " b: " << b << " i: " << i;
i = 30;
std::cout << "a: " << a << " b: " << b << " i: " << i;
i = &b;
std::cout << "a: " << a << " b: " << b << " i: " << i;
b = &a;
std::cout << "a: " << a << " b: " << b << " i: " << i;
a = &i; // Creating a circular loop?
std::cout << "a: " << a << " b: " << b << " i: " << i;
Java does not have double referencing or referencing as I understand the
C++ spec (I, unfortunately, no longer have my draft copy of the spec).
Every non-primitive type is closer to a pointer in JVM implementations
(double-pointers, actually, under most implementations). I think PHP5
tends to follow the C++ way. I don't know anything about Ruby and I
think that Python follows the Java style of what's going on.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth