fork download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5. class A
  6. {
  7. public:
  8. A(int i) : m_i(i) {}
  9. int geti() { return m_i; }
  10.  
  11. private:
  12. int m_i;
  13. };
  14.  
  15. int main() {
  16. auto a = std::make_shared<A>(123);
  17. auto b = a;
  18. std::cout << a->geti() << std::endl;
  19. std::cout << b->geti() << std::endl;
  20. b = nullptr; //use b.reset(); instead
  21. std::cout << a->geti() << std::endl; // This still works
  22. //std::cout << b->geti() << std::endl; // This will crash
  23. return 0;
  24. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
123
123
123