3 ms·
No, it really is different. In C++ you can assign to variable references without additional syntax. You cannot do this (at all) in Java. I don't know what you
by colanderman 9y ago
No, it really is different. In C++ you can assign to variable references without additional syntax. You cannot do this (at all) in Java.
I don't know what you mean by "really pass-by-reference". There is no such concept of a "reference" at the CPU level; nor is that relevant to language semantics. If you were to look at the generated assembly it would look identical to as if pointer referencing and dereferencing were used.
- ThoAppelsin 9y ago> I don't know what you mean by "really pass-by-reference". I was wondering if `int &a2 = a;` allocates memory in stack to make `a2` refer to `&a`, or does `a2` somehow truly become an alias for `a` in symbol table. As you've said that the assembly would look the same as pointers being dereferenced, then I suppose C++'s reference types are just a syntax sugar around the C's way of achieving the pass-by-reference concept through passing the address and then dereferencing it.
- chi3 9y agoUnless I'm misunderstanding you, what you're asking is only possible in a function signature if the compiler inlines it. However, outside of function signatures, if you write: int a = 2; int &aref = a; int b = aref; I'd assume that the compiler is smart enough to optimize away &aref.