fork download
  1. #include <iostream>
  2.  
  3. template<class Derived>
  4. class Singleton {
  5. public:
  6. static Derived& instance() {
  7. static Derived theInstance;
  8. return theInstance;
  9. }
  10.  
  11. protected:
  12. Singleton() {}
  13.  
  14. private:
  15. Singleton(const Singleton<Derived>&);
  16. Singleton<Derived>& operator=(const Singleton<Derived>&);
  17. };
  18.  
  19. class ASingleton : public Singleton<ASingleton> {
  20. public:
  21. void foo() { std::cout << "foo() called ..." << std::endl; }
  22. };
  23.  
  24. int main() {
  25. ASingleton& a = ASingleton::instance();
  26. a.foo();
  27. return 0;
  28. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
foo() called ...