fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. struct B;
  5. struct A
  6. {
  7. void print() const { std::cout << "A " << std::endl; }
  8. std::weak_ptr<B> pB;
  9. };
  10.  
  11. struct B
  12. {
  13. void print() const { std::cout << "B " << std::endl; }
  14. std::weak_ptr<A> pA;
  15. };
  16.  
  17. int main()
  18. {
  19. auto a = std::make_shared<A>();
  20. auto b = std::make_shared<B>();
  21.  
  22. a->pB = b;
  23. b->pA = a;
  24. a->print();
  25. auto wb = a->pB.lock();
  26. if (wb) { wb->print(); } else { std::cout << "nullptr\n"; }
  27.  
  28. a.reset();
  29.  
  30. auto wa = b->pA.lock();
  31. if (wa) { wa->print(); } else { std::cout << "nullptr\n"; }
  32. }
  33.  
Success #stdin #stdout 0s 4496KB
stdin
Standard input is empty
stdout
A 
B 
nullptr