Created
July 17, 2015 13:42
-
-
Save spitz-dan-l/a497186c24c4ccd3f66b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Pointers | |
int a = 3, b = 4; | |
int c = 1, d = 2; | |
int* pa = &a; | |
int e = *pa; | |
*pa == a; | |
pa == &a; | |
*pa = 2; | |
a == 2; | |
// Ignore everything above now | |
// What comes next has nothing to do with the & that you see above. | |
int a = 3, b = 4; | |
int c = 1, d = 2; | |
int& ra = a; | |
int e = ra; | |
ra = 2; | |
ra == a; | |
swap_ref(a, b); | |
a == 4; | |
b == 3; | |
void swap_ref(int& v1, int& v2){ | |
int temp = v1; | |
v1 = v2 | |
v2 = temp; | |
} | |
swap_ptr(&c, &d); | |
void swap_ptr(int* v1, int* v2){ | |
int temp = *v1; | |
*v1 = *v2; | |
*v2 = temp; | |
int *v3 = v2; | |
*v3 = *v2; | |
int** v4 = &v3; | |
v4 = &v1; | |
*v4 = v2; | |
**v4 = 5; | |
} | |
int x = 5; | |
int* px = &x; | |
int** ppx = &px; | |
*px = 7; | |
x == 7; | |
**ppx = 6; | |
x == 6; | |
foo(a, b); | |
void foo(int a, int b){ | |
//blargh | |
} | |
void foo(int& a, int& b){ | |
//insidious in-place modification of a and b | |
a = 7; | |
b = 100001; | |
} | |
int *p1 = &a; | |
p1 = &b; | |
a != b; | |
int &r1 = a; | |
r1 = b; | |
a == b; | |
// Java | |
Foo a = new Foo(3); | |
Foo e = a; | |
e == a; | |
e = new Foo(2); | |
e != a; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment