fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. template <typename T>
  5. class SKOptionalPtr : public std::shared_ptr<T>
  6. {
  7. public:
  8. // inherited constructors.
  9. using std::shared_ptr<T>::shared_ptr;
  10.  
  11. void weak(T* object)
  12. {
  13. this->reset(object, [] (T*) {});
  14. }
  15. };
  16.  
  17. int main() {
  18. SKOptionalPtr<int> ptr;
  19.  
  20. // pointeur à détruire
  21. ptr.reset(new int(1));
  22.  
  23. // pointer à ne pas détruire
  24. int already_allocated = 1234;
  25. ptr.weak(&already_allocated);
  26.  
  27. std::cout << *ptr << std::endl;
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1234