1 /* 2 * Copyright (C) 2015-2019 Alibaba Group Holding Limited 3 */ 4 #include <iostream> 5 6 using namespace std; 7 8 class singleton { 9 public: get_instance()10 static singleton *get_instance() 11 { 12 static singleton single; 13 return &single; 14 } get_value()15 int get_value() { return _value; }; set_value(int value)16 void set_value(int value) { _value = value; } 17 18 private: 19 int _value; singleton()20 singleton() : _value(0) {} ~singleton()21 ~singleton() {} 22 }; 23 static_singleton_test(void)24void static_singleton_test(void) 25 { 26 cout << "static singleton test start" << endl; 27 singleton *single0 = singleton::get_instance(); 28 singleton *single1 = singleton::get_instance(); 29 single0->set_value(10); 30 cout << "single0 set value 10, single1 get vale is " << single1->get_value() 31 << endl; 32 33 if (single0 != single1) { 34 cout << "static singleton test error" << endl; 35 } else { 36 cout << "static singleton test ok" << endl; 37 } 38 } 39