#include <iostream>
using namespace std;
class MyClass {
int x;
public:
MyClass (int val) : x(val) {
cout << "constructed :" << this << endl;
}
int& get() {
cout << "x is : " << x << endl ;
return x;
}
};
int main () {
MyClass foo = {10};
foo.get() = 20;
foo.get();
foo = 30;
cout << &foo << endl;
foo.get();
}
I get the following output:
constructed :0x7fffc44ef920
x is : 10
x is : 20
constructed :0x7fffc44ef924
0x7fffc44ef920
x is : 30
Why is this foo.get() = 20
a legitimate way of changing the value of 'x' in the object foo?
Also why is this foo = 30
constructing a new object at the address 0x7fffc44ef924
? What is this object type?
Also why does this: cout << &foo << endl;
print the address of the original MyClass foo object instantiated at the beginning of the main function (at address 0x7fffc44ef920
)?
How do you reference the 'new' foo at the address 0x7fffc44ef924
?