#include <iostream>
using namespace std;
class Foobar {
public:
Foobar(int x) : x(x) {
}
void frobnicate_1() {
cout << "In frobnicate_1, this=" << this << endl;
}
void frobnicate_2() {
cout << "In frobnicate_2, this=" << this
<< ", y=" << Foobar::y // doesn’t lookup “this”
<< ", x=" << flush;
cout << this->x << endl; // crash, if “this” isn’t valid
}
private:
int x;
static int y;
};
int Foobar::y = -3;
int main() {
Foobar a(5);
Foobar* f = &a;
f->frobnicate_1();
f->frobnicate_2();
f = nullptr;
f->frobnicate_1();
f->frobnicate_2(); // crash!
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCgpjbGFzcyBGb29iYXIgewpwdWJsaWM6CglGb29iYXIoaW50IHgpIDogeCh4KSB7Cgl9CgoJdm9pZCBmcm9ibmljYXRlXzEoKSB7CgkJY291dCA8PCAiSW4gZnJvYm5pY2F0ZV8xLCB0aGlzPSIgPDwgdGhpcyA8PCBlbmRsOwoJfQoKCXZvaWQgZnJvYm5pY2F0ZV8yKCkgewoJCWNvdXQgPDwgIkluIGZyb2JuaWNhdGVfMiwgdGhpcz0iIDw8IHRoaXMKCQkJIDw8ICIsIHk9IiA8PCBGb29iYXI6OnkgIC8vIGRvZXNu4oCZdCBsb29rdXAg4oCcdGhpc+KAnQoJCQkgPDwgIiwgeD0iIDw8IGZsdXNoOwoKCQljb3V0IDw8IHRoaXMtPnggPDwgZW5kbDsgIC8vIGNyYXNoLCBpZiDigJx0aGlz4oCdIGlzbuKAmXQgdmFsaWQKCX0KcHJpdmF0ZToKCWludCB4OwoJc3RhdGljIGludCB5Owp9OwoKaW50IEZvb2Jhcjo6eSA9IC0zOwoKCmludCBtYWluKCkgewoJRm9vYmFyIGEoNSk7CgoJRm9vYmFyKiBmID0gJmE7CglmLT5mcm9ibmljYXRlXzEoKTsKCWYtPmZyb2JuaWNhdGVfMigpOwoKCWYgPSBudWxscHRyOwoJZi0+ZnJvYm5pY2F0ZV8xKCk7CglmLT5mcm9ibmljYXRlXzIoKTsgLy8gY3Jhc2ghCn0K
In frobnicate_1, this=0xff81fc9c
In frobnicate_2, this=0xff81fc9c, y=-3, x=5
In frobnicate_1, this=0
In frobnicate_2, this=0, y=-3, x=