fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <functional>
  4.  
  5. class Object
  6. {
  7. virtual ~Object() { std::cout << "Object::~Object()" << std::endl; }
  8.  
  9. public:
  10. static Object* instance()
  11. {
  12. static std::unique_ptr<Object, std::function<void(Object*)>> instance(nullptr, [](Object *object) { delete object; });
  13. if (!instance)
  14. instance.reset(new Object);
  15. return instance.get();
  16. }
  17.  
  18. void test()
  19. {
  20. std::cout << "Object::test()" << std::endl;
  21. }
  22. };
  23.  
  24. int main()
  25. {
  26. Object::instance()->test();
  27. }
Success #stdin #stdout 0s 4176KB
stdin
Standard input is empty
stdout
Object::test()
Object::~Object()