fork(2) download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. class Hoge
  5. {
  6. public:
  7. static Hoge& GetInstance();
  8.  
  9. void Hey()
  10. {
  11. std::cout << foo << std::endl;
  12. }
  13. void SetFoo( int a )
  14. {
  15. foo = a;
  16. }
  17.  
  18. private:
  19. Hoge() : foo(0) {};
  20. Hoge( const Hoge& a ) {};
  21. ~Hoge() {};
  22. struct hoge_deleter;
  23.  
  24. int foo;
  25. };
  26.  
  27. struct Hoge::hoge_deleter
  28. {
  29. void operator()( const Hoge* const p )
  30. {
  31. std::cout << "Hoge deleter" << std::endl;
  32. delete p;
  33. }
  34. };
  35.  
  36. Hoge& Hoge::GetInstance()
  37. {
  38. static std::unique_ptr<Hoge, hoge_deleter> instance( new Hoge() );
  39. return *instance;
  40. }
  41.  
  42. int main()
  43. {
  44. Hoge& hoge1 = Hoge::GetInstance();
  45. hoge1.SetFoo( 2 );
  46. Hoge& hoge2 = Hoge::GetInstance();
  47.  
  48. hoge1.Hey();
  49. hoge2.Hey();
  50. }
Success #stdin #stdout 0s 2960KB
stdin
Standard input is empty
stdout
2
2
Hoge deleter