fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. class Test {
  5. public:
  6. Test(){}
  7. Test(int n) : num(n) {}
  8. int num;
  9. };
  10.  
  11. template<typename T>
  12. class Object {
  13. public:
  14. Object(const T& v) : value(std::make_shared(v)) {}
  15. Object(std::unique_ptr<T>&& v) : value(std::move(v)) {}
  16.  
  17. T* operator->() { return value.get(); }
  18. private:
  19. std::shared_ptr<T> value;
  20. };
  21.  
  22. int main(int argc, char *argv[]) {
  23.  
  24.  
  25. Object<Test> object = std::make_unique<Test>();
  26. object->num = 67;
  27. std::cout << object->num << std::endl;
  28.  
  29. {
  30. Object<Test> ot = object;
  31. ot->num = 34;
  32. }
  33. std::cout << object->num << std::endl;
  34. }
  35.  
Success #stdin #stdout 0s 4272KB
stdin
Standard input is empty
stdout
67
34