Skip to content

Instantly share code, notes, and snippets.

@Finwood
Created February 28, 2017 20:13
Show Gist options
  • Save Finwood/0516c75a255891bad02a8629cbf315e5 to your computer and use it in GitHub Desktop.
Save Finwood/0516c75a255891bad02a8629cbf315e5 to your computer and use it in GitHub Desktop.
C++ pointers (and references), once and for all!
// `foo` is a completely normal integer.
int foo = 42;
// `bar` is a pointer to an integer. Right now it points to `foo`'s address,
// but may later be reassigned to other addresses.
int *bar = &foo;
// `baz` is a reference to an integer; behaving like a normal integer in
// day-to-day life, only sharing address (and value, obviously) with `foo`.
int &baz = foo;
// Changing `*bar` also changes the value of `foo`:
*bar += 1; // foo == 43
// Likewise does changing `baz`:
baz -= 1; // foo == *bar == 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment