fork(4) download
  1.  
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <queue>
  7. #include <vector>
  8. #include <list>
  9. #include <functional>
  10. #include <memory>
  11.  
  12. struct Son {
  13. // some data that we want to point to
  14. ~Son(){printf("Bye Son\n");}
  15. };
  16.  
  17. struct Father {
  18. Son son;
  19. Father(){}
  20. ~Father(){printf("Bye Father\n");}
  21. };
  22.  
  23. int main(){
  24. std::shared_ptr<Father> father = std::make_shared<Father>();
  25. std::shared_ptr<Son> son(father, &father->son);
  26. printf("%d\n", father.use_count());
  27. printf("%d\n", son.use_count());
  28. father.reset();
  29. printf("%d\n", father.use_count());
  30. printf("%d\n", son.use_count());
  31. printf("%d\n", father.owner_before(son));
  32. printf("%d\n", son.owner_before(father));
  33. }
  34.  
Success #stdin #stdout 0s 5620KB
stdin
Standard input is empty
stdout
2
2
0
1
1
0
Bye Father
Bye Son