Created
May 7, 2020 01:14
-
-
Save mshafae/fa3c0ad15c6ad1c72508f9e471a4ae97 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
#include <iostream> | |
using namespace std; | |
struct Foo{ | |
int a; | |
char b; | |
}; | |
int main(void){ | |
Foo x = {42, 'x'}; | |
cout << "Foo x is (" << x.a << ", " << x.b << ") "; | |
cout << "and it is located at " << &x << endl; | |
cout << endl; | |
Foo* p = &x; | |
cout << "Let's set Foo* p equal to the address of x..." << endl; | |
cout << "Foo *p is " << p << " and it is located at " << &p << endl; | |
// Can't say this cout << "By dereferencing p, p points to " << *p << endl; | |
cout << "By dereferencing the parts of p using the -> operator" << endl; | |
cout << "p->a is " << p->a << endl; | |
cout << "and p-> is " << p->b << endl; | |
cout << endl; | |
Foo& r = x; | |
cout << "Let's set Foo& r equal to x..." << endl; | |
cout << "Foo& r is (" << r.a << ", " << r.b << ") and it is located at " << &r << endl; | |
cout << "Notice that r (" << &r << ") has the same address as x (" << &x << ")" << endl; | |
cout << "This is very strange since x and r are two different things." << endl; | |
cout << endl; | |
cout << "Let's set p equal to the address of r..." << endl; | |
p = &r; | |
cout << "Foo *p is " << p << " and it is located at " << &p << endl; | |
//Can't say this cout << "By dereferencing p, p points to " << *p << endl; | |
cout << "By dereferencing the parts of p using the -> operator" << endl; | |
cout << "p->a is " << p->a << endl; | |
cout << "and p-> is " << p->b << endl; | |
cout << endl; | |
cout << endl; | |
Foo y = {17, 'z'}; | |
cout << "Foo y is (" << y.a << ", " << y.b << ") "; | |
cout << "and it is located at " << &y << endl; | |
cout << endl; | |
cout << "Can I have p point to y?" << endl; | |
p = &y; | |
cout << "Foo *p is " << p << " and it is located at " << &p << endl; | |
//cout << "By dereferencing p, p points to " << *p << endl; | |
cout << "By dereferencing the parts of p using the -> operator" << endl; | |
cout << "p->a is " << p->a << endl; | |
cout << "and p-> is " << p->b << endl; | |
cout << "The answer is yes I can!" << endl; | |
cout << endl; | |
cout << "Can I have r bind to y?" << endl; | |
r = y; | |
cout << "Let's set Foo& r equal to y..." << endl; | |
cout << "Foo& r is (" << r.a << ", " << r.b << ") and it is located at " << &r << endl; | |
cout << "Notice that r (" << &r << ") has the same address as x (" << &x << ") still!!!!" << endl; | |
cout << "What just happened? (You can't rebind a reference.)" << endl; | |
cout << "Foo x was changed to be equal to Foo y." << endl; | |
cout << "Foo x is (" << x.a << ", " << x.b << ") "; | |
cout << "and it is located at " << &x << endl; | |
cout << "Foo y is (" << y.a << ", " << y.b << ") "; | |
cout << "and it is located at " << &y << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment