Created
July 29, 2019 09:39
-
-
Save sprytnyk/713c4ed1c781aee8cbbf748306765c4f to your computer and use it in GitHub Desktop.
Pointers & references in Python & CPP (rough analogues).
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
#include <iostream> | |
using namespace std; | |
int main() { | |
// References in work. | |
int x(0); | |
int &ref = x; | |
x = 10; | |
cout << x << endl; | |
cout << ref << endl; | |
cout << &ref << endl; | |
/* | |
Python analogue: | |
i = 1 | |
j = i | |
hex(id(i)) == j hex(id(j)) | |
i & j have the same memory addresses (share it) | |
*/ | |
// Pointers in work. | |
int y(10); | |
int *point = &y; | |
cout << endl; | |
cout << y << endl; | |
cout << point << endl; | |
y = 20; | |
cout << y << endl; | |
cout << point << endl; | |
/* | |
Python analogue: | |
i = 1 | |
j = hex(id(i)) | |
hex(id(i)) != hex(id(j)) | |
i & j memory addresses aren't coincide but j holds memory address of i | |
*/ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment