@SouleDancer: Regarding your example of passing a pointer by value:
SouleDancer wrote: |
---|
If you pass a reference to a variable like f( &in ), you can change "in", but you cannot change the passed value (a manual reference) so that it is remembered when the function returns. |
Why would you want to modify the temporary? :P
Regarding you saying pass by reference is bad: Most C++ implementations do references via hidden pointers. This usually ends up with them passing the hidden pointer by value, just as in your example.
1 2 3 4 5 6
|
class Blah
{
char &ref;
public:
Blah(char &c) : ref(c) {}
};
|
1 2
|
cout << sizeof(char &) << endl;
cout << sizeof(Blah) << endl;
|
http://ideone.com/fS88h
I have something to ask you (unrelated to above) or anyone who knows. I am trying to learn the "Java way" of things, so this may seem stupid.
In Java, you only have mutable and immutable types, but that is not defined by any language feature - it is defined only by the choice of whether to allow change through methods. What about an ArrayList? How can I guarantee that the original is not changed without making an expensive copy or deep copy?
This is a 'this language has and this language doesn't have' question. C++ has const-correctness without expensive copy. Java has cross-your-fingers-or-make-expensive-copies.
Don't tell me to extend ArrayList and override the methods that change it-what if I need to do this for a
final class? What if one of its elements is modified by the reference returned from get()?
My question: Does Java support
optional const-correctness without copying? And how?
Other than const correctness I'm perfectly fine with everything in Java (it took me a while to say that though). Still learning though, and remember I come from C++ on this view of Java.