3 ms·
my 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
by Tobani 12y ago
my 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.