5 ms·
So would you assert this is not pass-by-reference? void f(const Foo& x){ ... } My point is the semantics don't map 1:1. If you're going to say Ja
by Tobani 12y ago
So would you assert this is not pass-by-reference?
void f(const Foo& x){
...
}
My point is the semantics don't map 1:1. If you're going to say Java's references are pointers with different semantics how is that semantically different from saying Java's references are references with slightly different semantics?
- geocar 12y agovoid f(const Foo&_x){ Foo& x=const_cast<Foo&> (_x); … } What do you think is going on here?
- Tobani 12y ago> What do you think is going on here? I think you're adding code that doesn't address my point. This is pointer manipulation, not function call semantics. If you have raw pointers you can do a lot of magic. I think your code actually demonstrates how not-pointer like Java's references actually are.
- AnthonyMouse 12y agoMaking the referenced object const doesn't change anything. Fill in the '?': void f(Foo* x) { Foo y; *x = y; } void f(Foo& x) { Foo y; x = y; } void f(const Foo* x) { const Foo y; x = &y; } void f(const Foo& x) { const Foo y; ?; }
- Tobani 12y agomy cpp foo is much out of date admittedly. I believe my comment explicilty deals with its parent comment. I believe your example you are setting the contents of a specific memory address. This is nonsensical in a java context. Again another example of how semantics dont map 1:1.
- AnthonyMouse 12y agoThey do map 1:1, that's the point: void f(Foo* x) { Foo* y = new Foo(); x = y; } // C++ public void f(Foo x) { Foo y = new Foo(); x = y; } // Java If 'x' was a reference then you couldn't do that.
- Tobani 12y agoExcept x could be an array
- eslaught 12y agoExcept you still have no way to modify the variable binding on the caller's stack. This is what all of us in the "Java is call-by-value" camp keep yammering on about: changing a value on the heap (an array or object in Java) is not the same as changing a variable on the stack (a local variable in Java). In Java, you can never touch local variables on parent functions' stacks, therefore, Java cannot be considered pass-by-reference. The rest of it (const, const_cast, arrays-are-kinda-pointers in C++, taking the address of stack variables, etc.) are just weird quirks in the language semantics. (Honestly, C++ is already a quirky enough language on its own to make detailed comparisons inherently problematic.) At the most fundamental level, C++ supports both call-by-value and call-by-reference, while Java only supports call-by-value.
- Tobani 12y agoI get what you're saying. I'm just saying philosophically if you're going to say it is pass-by-pointer w/ castrated pointers is that any different than pass-by-reference with castrated references? To blur the lines: If escape analysis is enabled its entirely possible for an object to allocated on the stack. In this situation a callee could manipulate the parent stack. Granted it can't overwrite the entire region wholesale, but you could functionally overwrite it all, but java doesn't have any way of doing that semantically anyway.
- jjnoakes 12y agoSemantics don't have to map 1:1 to still have a meaningful relationship. Java object references are mostly like C++ pointers. Even though you can't do arithmetic on them, the way they behave during assignment and parameter passing is identical. The primary qualities of a pointer are that it points to something, copies of it are shallow (and cheap) and point to the same thing, equality of pointers implies equality of the objects (but not vice versa), etc. All the same in both languages. Nothing in Java is like C++ references (except the word reference) because the primary qualities of references are that they alias the objects that they refer to, they can not be rebound, and operations on them act exactly the same as operations on the aliased objects, with no dereferencing operators necessary, including assignment. Once you understand that, you can begin to grasp how Java can be fully pass-by-value and yet your function can modify a list and the caller will see the change.