fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. int main() {
  5. std::shared_ptr<int> p1( new int( 0 ) );
  6. std::shared_ptr<int> p2( new int( 1 ) );
  7.  
  8. std::cout << "-----step1-----" << std::endl;
  9. std::cout << "p1 = " << *p1 << std::endl;
  10. std::cout << "p2 = " << *p2 << std::endl;
  11.  
  12. std::cout << std::endl << "-----step2-----" << std::endl;
  13. std::shared_ptr<int> p3 = p1;
  14. std::cout << "p1 = " << *p1 << std::endl;
  15. std::cout << "p2 = " << *p2 << std::endl;
  16. std::cout << "p3 = " << *p3 << std::endl;
  17.  
  18. std::cout << std::endl << "-----step3-----" << std::endl;
  19. p3 = p2;
  20. std::cout << "p1 = " << *p1 << std::endl;
  21. std::cout << "p2 = " << *p2 << std::endl;
  22. std::cout << "p3 = " << *p3 << std::endl;
  23.  
  24. std::cout << std::endl << "-----step4-----" << std::endl;
  25. p1.reset();
  26. p2.reset();
  27. std::cout << "p1 is " << ( ( (bool)p1 ) ? "not nullptr" : "nullptr" ) << std::endl;
  28. std::cout << "p2 is " << ( ( (bool)p2 ) ? "not nullptr" : "nullptr" ) << std::endl;
  29. std::cout << "p3 = " << *p3 << std::endl;
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
-----step1-----
p1 = 0
p2 = 1

-----step2-----
p1 = 0
p2 = 1
p3 = 0

-----step3-----
p1 = 0
p2 = 1
p3 = 1

-----step4-----
p1 is nullptr
p2 is nullptr
p3 = 1