fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. struct Connection {
  9. Connection() {
  10. cout << "connection open" << endl;
  11. }
  12. ~Connection() {
  13. cout << "connection closed" << endl;
  14. }
  15. };
  16.  
  17.  
  18. struct Reader {
  19. Reader(shared_ptr<Connection> c) :
  20. c(c) {
  21. cout << "Reader created" << endl;
  22. }
  23. ~Reader() {
  24. cout << "Reader deleted" << endl;
  25. }
  26. shared_ptr<Connection> c;
  27. };
  28.  
  29.  
  30. struct Holder {
  31. shared_ptr<Connection> c;
  32. Reader* r;
  33. Holder() :
  34. c(make_unique<Connection>()) {
  35. cout << "Holder created" << endl;
  36. r = new Reader(c);
  37. }
  38. ~Holder() {
  39. if (r) { delete r; }
  40. cout << "Holder deleted" << endl;
  41. }
  42. };
  43.  
  44.  
  45. int main() {
  46. {
  47. Holder h;
  48. }
  49.  
  50.  
  51. {
  52. Holder h;
  53. }
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
connection open
Holder created
Reader created
Reader deleted
Holder deleted
connection closed
connection open
Holder created
Reader created
Reader deleted
Holder deleted
connection closed