fork download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4. class Foo : public enable_shared_from_this<Foo>
  5. {
  6. Foo() = default;
  7. /*should be virtual ?????*/
  8. ~Foo()
  9. {
  10. cout << "des" << endl;
  11. }
  12.  
  13.  
  14. public:
  15.  
  16. static shared_ptr<Foo> Create() {
  17. class PrivateFoo : public Foo { /*empty*/ };
  18. return std::make_shared<PrivateFoo>();
  19. }
  20. void Test()
  21. {
  22. auto sp = shared_from_this();
  23. cout << sp.use_count() <<endl;
  24. }
  25. };
  26.  
  27. int main()
  28. {
  29. auto f = Foo::Create();
  30. f->Test();
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 4536KB
stdin
Standard input is empty
stdout
2
des