fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class Foobar {
  7. public:
  8. Foobar(int x) : x(x) {
  9. }
  10.  
  11. void frobnicate_1() {
  12. cout << "In frobnicate_1, this=" << this << endl;
  13. }
  14.  
  15. void frobnicate_2() {
  16. cout << "In frobnicate_2, this=" << this
  17. << ", y=" << Foobar::y // doesn’t lookup “this”
  18. << ", x=" << flush;
  19.  
  20. cout << this->x << endl; // crash, if “this” isn’t valid
  21. }
  22. private:
  23. int x;
  24. static int y;
  25. };
  26.  
  27. int Foobar::y = -3;
  28.  
  29.  
  30. int main() {
  31. Foobar a(5);
  32.  
  33. Foobar* f = &a;
  34. f->frobnicate_1();
  35. f->frobnicate_2();
  36.  
  37. f = nullptr;
  38. f->frobnicate_1();
  39. f->frobnicate_2(); // crash!
  40. }
  41.  
Runtime error #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
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=