fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. class Object
  5. {
  6. public:
  7. //...potentially lots of data...
  8. };
  9.  
  10. class ObjectHandle
  11. {
  12. public:
  13. ObjectHandle() { std::cout << "Constructed" << std::endl; }
  14. ~ObjectHandle() { std::cout << "Destructed" << std::endl; }
  15.  
  16. private:
  17. Object *ptrToPotentiallyLotsOfData = nullptr;
  18. };
  19.  
  20. int main()
  21. {
  22. std::cout << "ObjectHandle size: " << sizeof(ObjectHandle) << std::endl;
  23. std::cout << (std::is_pod<ObjectHandle>::value? "Is plain-ol`-data" : "Is not plain-old-data") << std::endl;
  24. std::cout << "\nScope began" << std::endl;
  25. {
  26. std::shared_ptr<ObjectHandle> objectSPtr = std::make_shared<ObjectHandle>();
  27. std::weak_ptr<ObjectHandle> objectWPtr = objectSPtr;
  28.  
  29. std::cout << "Resetting shared_ptr" << std::endl;
  30. objectSPtr.reset();
  31. std::cout << "Done resetting shared_ptr" << std::endl;
  32.  
  33. }
  34. std::cout << "Scope ended" << std::endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
ObjectHandle size: 4
Is not plain-old-data

Scope began
Constructed
Resetting shared_ptr
Destructed
Done resetting shared_ptr
Scope ended