// Online C++ compiler to run C++ program online
#include <iostream>
class X {
public:
X() { std::cout << "X constructed" << std::endl; }
~X() { std::cout << "X destroyed" << std::endl; }
X& get() { return *this; }
int get_i() {
std::cout << "getting i" << std::endl;
return i;
}
private:
int i = 7;
};
int main() {
int val = X().get().get_i();
std::cout << val << std::endl;
std::cout << "now lets break it" << std::endl;
auto& x = X().get();
// This is accessing destroyed mem.
std::cout << x.get_i();
return 0;
}