关于C++单例类模板详解

本文主要介绍了C++单例类模板,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着微点阅读小编来一起学习学习吧

单例类

描述

指在整个系统生命期中,一个类最多只能有一个实例(instance)存在,使得该实例的唯一性(实例是指一个对象指针)  , 比如:统计在线人数

在单例类里,又分为了懒汉式和饿汉式,它们的区别在于创建实例的时间不同:

  1. 懒汉式 : 指代码运行后,实例并不存在,只有当需要时,才去创建实例(适用于单线程)
  2. 饿汉式 : 指代码一运行,实例已经存在,当时需要时,直接去调用即可(适用于多线程)

用法

  1. 将构造函数的访问属性设置为private,
  2. 提供一个GetInstance()静态成员函数,只能供用户访问唯一一个实例.
  3. 定义一个静态成员指针,用来供用户获取
  4. 重载 (=)赋值操作符以及拷贝构造函数,并设为private, 避免对象间拷贝,复制.

初探单例类-懒汉式:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

#include <iostream>

using namespace std;

class CSingleton

{

private:

    static CSingleton* m_pInstance;

    CSingleton()     //构造函数为private

    {

    }

    CSingleton& operator = (const CSingleton& t);

    CSingleton(const CSingleton &);

public:

    static CSingleton* getInstance()

    {

       if(m_pInstance==NULL)

           m_pInstance= new CSingleton();

       

        return m_pInstance;

    }

    void print()

    {

      cout<<this<<endl;  

    }

};

CSingleton* CSingleton::m_pInstance = NULL;

int main()

{

    CSingleton *p1=CSingleton::getInstance();

    CSingleton *p2=CSingleton::getInstance();

    CSingleton *p3=CSingleton::getInstance();

    p1->print();

    p2->print();

    p3->print();

    return 0;

}

运行打印:

0x6e2d18
0x6e2d18
0x6e2d18

从打印结果可以看出,该指针对象指向的都是同一个地址,实现了一个类最多只能有一个实例(instance)存在.

注意:由于实例(instance),在系统生命期中,都是存在的,所以只要系统还在运行,就不需要delete

上面的懒汉式如果在多线程情况下 ,多个Csingleton指针对象同时调用getInstance()成员函数时,由于m_pInstance = NULL,就会创建多个实例出来.

所以,在多线程情况下,需要使用饿汉实现

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

class CSingleton

{

private:

    static CSingleton* m_pInstance;

    CSingleton()     //构造函数为private

    {

    }      

    CSingleton& operator = (const CSingleton& t);

    CSingleton(const CSingleton &);

public:

    static CSingleton* getInstance()

    {

        return m_pInstance;

    }

};

CSingleton* CSingleton::m_pInstance = new CSingleton;

单例类模板

我们现在讲解的仅仅是个框架,里面什么都没有,不能满足需求啊,所以还要写为单例类模板头文件,当需要单例类时,直接声明单例类模板头文件即可

写CSingleton.h

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#ifndef _SINGLETON_H_

#define _SINGLETON_H_

template <typename T>

class CSingleton

{

private:

    static T* m_pInstance;

    CSingleton()     //构造函数为private

    {  

     }   

     

public:

    static T* getInstance()

    

        return m_pInstance;

    }

};

template <typename T>

T* CSingleton<T> :: m_pInstance = new T;

#endif

当我们需要这个单例类模板时,只需要在自己类里通过friend添加为友元即可,

接下来试验单例类模板

写main.cpp

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

#include <iostream>

#include <string>

#include "CSingleton.h"

using namespace std;

class Test

{

    friend class CSingleton<Test> ; //声明Test的友元为单例类模板

private

    string mstr;

    Test(): mstr("abc")

    {

    }

    Test& operator = (const Test& t);

    Test(const Test&);

public

    void Setmstr(string t)

    {

     mstr=t;

    }

    void print()

    {

    cout<<"mstr = "<<mstr<<endl;

    cout<<"this = "<<this<<endl;

    }

};

int main()

{

    Test *pt1 = CSingleton<Test>::getInstance();

    Test *pt2 = CSingleton<Test>::getInstance(); 

    pt1->print();

    pt2->print();

    pt1->Setmstr("ABCDEFG");

    pt2->print();

    return 0;

}

mstr = abc
this = 0x2d2e30

mstr = abc
this = 0x2d2e30

mstr = ABCDEFG
this = 0x2d2e30

以上所述是小编给大家介绍的C++单例类模板详解整合,希望对大家有所帮助

转自:微点阅读   https://www.weidianyuedu.com

猜你喜欢

转载自blog.csdn.net/weixin_45707610/article/details/131766695