Home

[Note to others: crossposted to [comp.lang.c++] and
[comp.lang.java.programmer]].

* Summercool:
> Can we confirm the following? also someone said, Java also has
> "reference" like in C++, which is an "implicit pointer":
>

"pointer", "reference", it's just terminology.

What these words mean depends on the programming language.

The special meaning of "reference" in C++ is unfortunate but a fact that
we must live with, it makes it very difficult to discuss these things in
general. Java does not have anything like it.

If you compare only the built-in types, a Java variable of class type, a
Java reference,

SomeType o = new SomeType(); // Java reference

(hope I got the syntax right, it's a long time since I did Java, and I'm
too lazy to put the examples to compilers!)

corresponds most closely to a C++ pointer,

SomeType* p = new SomeType(); // C++ pointer

but there is a huge difference in what you can do, apart from the syntax
issues: C++ supports pointers to pointers, which means that you can
write e.g. a function swap that exchanges the values of two pointer
variables (or any variables), while in Java that level of indirection is
only available for array elements, and then with run-time type checking.

Also, the Java reference provides guaranteed automatic garbage
collection (freeing memory when the object is no longer used), while in
C++ that's not guaranteed, and is in fact not common -- the language
doesn't prevent automatic garbage collection, but doesn't currently
support it either: automatic garbage collection for C++ exists, but is
more like experimental.

The C++ concept corresponding most closely to a Java reference is
therefore a smart pointer, an object that encapsulates a pointer and
provides garbage collection (not general automatic garbage collection
but reference-counted automatic garbage collection, which differs in
that it doesn't automatically deal with cycles of objects),

shared_ptr<SomeType> p( new SomeType() );

but that's not built-in, it's a library solution.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

previous
next

Re: Superclass and Subclasses
Re: exponential float formmating
Re: overloaded type cast functions
Re: newbie: self.member syntax seems /really/ annoying
I earn $36800 a month with google adsense
Podaruj Zycie
Akogo
Kidprotect
Rodzic Po Ludzku
Nasze Dzieci