Created
February 28, 2017 20:13
-
-
Save Finwood/0516c75a255891bad02a8629cbf315e5 to your computer and use it in GitHub Desktop.
C++ pointers (and references), once and for all!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// `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