Skip to content

Instantly share code, notes, and snippets.

@coldcue
Last active December 30, 2015 07:39
Show Gist options
  • Save coldcue/7797783 to your computer and use it in GitHub Desktop.
Save coldcue/7797783 to your computer and use it in GitHub Desktop.
//Java pass-by-value
public void foo(Dog d) {
d.getName().equals("Max"); // true
d = new Dog("Fifi");
d.getName().equals("Fifi"); // true
}
Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Max"); // true
//C++ corresponding (pass d reference by value)
public void foo(Dog &d) {
//d still points to a *Dog pointer
d->getName() == "Max"; //true
d = new Dog("Fifi");
d->getname() == "Fifi"; //true
}
Dog *aDog = new Dog("Max");
foo(&aDog);
aDog->getName() == "Max"; //true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment