?????????????????????????
????singleton pattern????????????????????????????????????????????????????????????????????????????????????????У??????????????????????????????????????????????????????????????????????????????????????
?????????????c++?У????????????????????????????????????????ù???????????????????????????????????????ù????????????????
????1??????????????????????????е? private??????????????????ù????????????????????
????2????????????????????????????????????????????????????????????????????????????
????3??????e??????????????? null???????public ?????????ж???????????????? null???????б???????????????????????????????????Ρ?
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????С?
??????????????????????????Σ?????????????
?????????????????? instance???????? get ?????????????????????????????????????????t???????????????????????????е? new ???????????????????????????? get ?????в? new instance????????????????????????????????????£???????? get ??????????? new ????????????????????
?????????????????????
1 //?????????
2 class Singleton
3 {
4 public:
5     static Singleton * getInstance()
6     {
7         if (instance == NULL) {
8             instance = new Singleton();
9         }
10
11         return instance;
12     }
13
14 private:
15     //??е?????????????????????
16     Singleton()
17     {
18         cout << "???????" << count << "??????" << endl;
19         count++;
20     }
21     //???????????????????????????t???????????????????о?????????????????????????????б????????
22     static Singleton *instance;
23     //?????????????
24     int count = 1;
25 };
26
27 Singleton * Singleton::instance = NULL;
28
29 int main(void)
30 {
31     Singleton::getInstance();
32     Singleton::getInstance();
33     Singleton::getInstance();
34     Singleton::getInstance();
35
36     return 0;
37 }
???????????1??????
????Program ended with exit code: 0
????С??
?????????????????????任????????????????????????任????
???????????????????????μ????????????
???????????????????????£?????????????????????????????????????????????????????????????????е????ж? if ???????????instance ???????б??????????????????????????в????????????????????????????????????????д??????????????