fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Object
  5. {
  6. virtual void print() = 0;
  7. };
  8.  
  9. class SingletonBase : public Object
  10. {
  11. private:
  12. static SingletonBase* theOnlyTrueInstance;
  13. protected:
  14. SingletonBase()
  15. {
  16. if(!theOnlyTrueInstance)
  17. theOnlyTrueInstance = this;
  18. }
  19. virtual ~SingletonBase(){}
  20. public:
  21. static SingletonBase* instance()
  22. {
  23. if (!theOnlyTrueInstance) initInstance();
  24. return theOnlyTrueInstance;
  25. }
  26. void print()
  27. { cout<<"Singleton"<<endl; }
  28. static void initInstance()
  29. { new SingletonBase; }
  30. };
  31.  
  32. SingletonBase* SingletonBase::theOnlyTrueInstance = 0;
  33.  
  34. class OtherBase : public Object
  35. {
  36. public:
  37. virtual string printSymbol() = 0;
  38. void print()
  39. { cout<<printSymbol(); }
  40. };
  41.  
  42. class SingletonChild : public SingletonBase , public OtherBase
  43. {
  44. public:
  45. string printSymbol()
  46. {
  47. return "Test";
  48. }
  49. static void initInstance()
  50. { new SingletonChild; }
  51. };
  52.  
  53. int main() {
  54. SingletonChild::initInstance();
  55. SingletonChild* test = (SingletonChild*)SingletonChild::instance();
  56. test->OtherBase::print();
  57. return 0;
  58. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
Test