[New Post] Learning C++ from Java - Pointers and References
Hello,
Thank you for subscribing to get notified when I publish a new post.
Here is a preview of my new post on pointers and references in C++, along with a link to the full content.
Learning C++ from Java - Pointers and References
Before I started learning C++, I had read about Java being pass-by-value rather than pass-by-reference, but I found it easier to think I was passing objects around by reference.
This post begins by summarizing pointers and references in C++, before describing the different semantics for passing arguments to a function.
…
Pass-by-Reference or Pass-by-Address
For complex data types where copying may be too expensive, the choice between pass-by-reference and pass-by-address is not straightforward.
Learn C++, for example, recommends sticking with pass-by-reference, but a more common recommendation I have found is to use pass-by-address when you are modifying the parameter in the function and pass by const
reference otherwise.
By using the latter approach, you are making it more explicit that an argument may be modified.
Java is Pass-by-Value
In Java, you only pass arguments by value, but this has the same semantics as pass-by-address in C++.
I mentioned earlier that with pass-by-address, you are actually passing the argument’s address by value, i.e. passing a copy of the address. This is the same in Java: you are passing a copy of a variable’s address.
Thank you,
Timi