?????????????????????????????????????????????????????????????????????????????
???????????????????????????????? instance???????? get ?????????????????????????????????????????t???????????????????????????е? new ????????????????????
1 class Singleton
2 {
3 public:
4     //get ????
5     static Singleton * getInstance(){
6         //?????????????????
7         return instance;
8     }
9 private:
10     //???????????????
11     static Singleton *instance;
12     //??й?????
13     Singleton(){
14
15     }
16 };
17 //?????????????instance??get ????????????????
18 Singleton * Singleton::instance = new Singleton();
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????????????????
????????????????? destory() ????????????ò??????????????????????????????????????????????????????????????????????delete?????????й??????????Щ??????????????????????????????????????????????????????????????????????????????????е???????????????????????????????С?
?????????????????????????????????????????е???????????????????????????е???????????????????Щ??????????????????????????????????????б??????????????????????????????????????????????????????????????????????????????????????????Щ?????????????????????ж????????????????????????????????????????????????????????????????????????е?Garbage??
1 class Singleton
2 {
3 public:
4     //get ????
5     static Singleton * getInstance(){
6         //?ж??????
7         if (NULL == instance) {
8             instance = new Singleton();
9         }
10         //?????????????????
11         return instance;
12     }
13     //c++ ???????????????????????????Garbage???????Singleton??????????????????????????á?
14     class Garbage
15     {
16     public:
17         ~Garbage(){
18             if (Singleton::instance != NULL) {
19                 cout << "??????????????????" << endl;
20                 delete Singleton::instance;
21             }
22         }
23     };
24
25 private:
26     //????????????????????????????????????????????????????????????????????????????????ó????????????????????????????????????????
27     static Garbage garbage;
28     //???????????????
29     static Singleton *instance;
30     //?????????й?????
31     Singleton(){
32         cout << "?????????????????" << endl;
33     }
34     //??????????????????
35     ~Singleton(){
36         cout << "????????????????????" << endl;
37     }
38 };
39 //?????????????????????????????????????????????????????????????
40 //??????????????????г?????????????????????г????
41 Singleton::Garbage Singleton::garbage;
42 //?????instance? null
43 Singleton * Singleton::instance = NULL;
44
45 int main(void)
46 {
47     Singleton *a = Singleton::getInstance();
48     Singleton *b = Singleton::getInstance();
49     Singleton *c = Singleton::getInstance();
50
51     if (a == b) {
52         cout << "a = b" << endl;
53     }
54
55     return 0;
56 }
?????????????????????
????a = b
??????????????????????
????????????????????????
????Program ended with exit code: 0
??????Garbage???????Singleton???????????????????????????????????н?????????????Singleton???????garbage??????????????????????????????????????????????????????????????????????
????1??????????????????е??????
????2????????????????е???????????????????
????3?????ó????????????????????????????????????????
????4?????????????????κβ????????????????????
?????????????????????????е????????????
?????????????????????????????????????ж???????????? null???????? null??????????????delete?????????????????????????????????????????a?????????????????????????????????????????????????????delete?????д????????GetInstance????????????????????????
??????????????????????????????????????????????????????????????
????1????????????????
????2??????????????
????3?????????
?????????????
????1??????????????????????????飺????????????????
????2????????????????????????飺???д????????????????????
????3?????????????????????????????У?????????????????????????????У???????????