I find myself in a situation where I need to refresh my C++ skills. This is one of a series of posts where I’ll cover some basics of C++.
If you’re decent at Java, chances are you understand pointers just fine, you just may or may not realize it. A common misconception I see in a wide range of Java developers, from students to even some professionals, is that Java passes primitive types by value and arrays, class, and interface types (reference types) by reference into methods. While it’s true that primitive types are passed into methods by value, the latter half of the previous sentence is not necessarily true. What happens is that when you pass in a reference type into a method, the reference itself is passed by value. What this means is that if you pass in an reference type into a method in Java, you can modify the object because the argument is a reference to that object. Since the reference is passed by value though, if you assign it to a new object (a new address), the original reference type will not be assigned to that new address. When the program exits out of that method, the old reference type (the object you passed in) will still be pointing to the original location in memory. Take a moment to let that sink in. Here’s some Java code to illustrate what I’m talking about.
public class PointerFun
{
public void manipulatePrimitiveValue(int x)
{
x = 10;
}
public void manipulateReferenceValue(StringBuilder s)
{
s.append(" World!");
s = new StringBuilder("How are you?");
}
public static void main(String[] args)
{
PointerFun p = new PointerFun();
int y = 5;
p.manipulatePrimitiveValue(y);
StringBuilder hello = new StringBuilder("Hello");
p.manipulateReferenceValue(hello);
System.out.println(y);
System.out.println(hello);
}
}
Since we know primitive values are passed into methods by value, we already know the first line printed out will be 5. What will the second line print? If reference types were passed by value, then it would be “Hello”, if they were passed by reference, then it would be “How are you?”. However, since the references themselves are passed by value, the second line will output “Hello World!”. The method takes in a copy of the reference to the object - now we have two references pointing to the same location in memory: the instance of StringBuilder we named “hello” in the main method, and the instance of StringBuilder named “s” in the manipulateReferenceValue method. Since s points to the same address as hello, we can use s to manipulate that object all we want. As soon as you assign s to a new StringBuilder object, it is no longer pointing towards the same address as hello. So changing s won’t do anything to the object pointed to by hello. Therefore, when we print out hello, it prints out the original object, which now has a value of “Hello World!”.
So, in Java you have already been exposed to a lot of the basics of pointer. So a healthy attitude to take is not to think that C++ is harder than Java in terms of pointers, but instead it is much more flexible. The only thing you really need to do is memorize the syntax. For instance:
#include <iostream>
void manipulateByValue(int x)
{
x = 10;
}
void manipulateByReference(int* x)
{
*x = 10;
}
int main()
{
int y = 5;
int z = 5;
manipulateByValue(y);
manipulateByReference(&z);
std::cout << y << std::endl;
std::cout << z << std::endl;
return 0;
}
Using pointer syntax you can pass any value by reference or by value. See? More flexible. I also recommend this tutorial for pointers. It’s one of the better tutorials out there.