欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++单例

程序员文章站 2022-04-21 20:51:25
1 template 2 class Singleton 3 { 4 public: 5 using object_type = T; 6 struct object...
1 template

2 class Singleton

3 {

4 public:

5 using object_type = T;

6 struct object_creator

7 {

8 object_creator()

9 {

10 Singleton::instance();

11 }

12 };

13

14 static object_creator creator_object;

15 public:

16 static object_type* instance()

17 {

18 static object_type _instance;

19 return(&_instance);

20 }

21 };

22 template typename Singleton::object_creator Singleton::creator_object;