我是靠谱客的博主 欢喜酒窝,这篇文章主要介绍使用C++ 11实现单例模式,现在分享给大家,希望可以做个参考。

C++ 单例模式在各种应用上使用都是比较广泛,但是在写单例模式的时候也需要考虑一些问题................

哎!不知道要怎么说了。  也不知道该如何解释了。  算了,直接上代码吧。

Singleton.hpp

复制代码
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
#ifndef _SINGLETON_H_ #define _SINGLETON_H_ #include <mutex> #include <memory> template<typename C> class Singleton { public: Singleton(const Singleton &) = delete; Singleton(Singleton &&) = delete; Singleton &operator=(const Singleton &) = delete; Singleton &operator=(Singleton &&) = delete; protected: Singleton() { } virtual ~Singleton() { } public: template<class... Args> static std::shared_ptr<C> Instance(Args&&... args) { static std::once_flag oc; std::call_once(oc, [&]{ instance = std::make_shared<C>(std::forward<Args>(args)...); }); return instance; } private: static std::shared_ptr<C> instance; }; template<typename C> std::shared_ptr<C> Singleton<C>::instance = nullptr; #endif // !_SINGLETON_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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef _SINGLETON_H_ #define _SINGLETON_H_ #include <mutex> #include <memory> template <typename C> class Singleton { public: static C *Instance() { if (!instance) { std::lock_guard<std::mutex> lck(mtx); if (!instance) { instance = new C(); //Destroy(); } return instance; } return instance; } protected: Singleton(void) = default; virtual ~Singleton(void) = default; private: Singleton(const Singleton &) = delete; Singleton(Singleton &&) = delete; Singleton &operator=(const Singleton &) = delete; Singleton &operator=(Singleton &&) = delete; static void Destroy() { if (instance) { delete instance; instance = nullptr; } } static C *instance; static std::mutex mtx; }; template <typename C> C *Singleton<C>::instance = nullptr; template <typename T> std::mutex Singleton<T>::mtx; #endif // !_SINGLETON_H_

看上去是不是非常简洁。这个是一个通用的单例模式模板。主要是使用了以下:

1. delete关键字,将构造系列函数变成私有,有人说把这些写进private,这样也是可以的,  但是我个人还是比较喜欢使用delete,而且也符合C++ 11以后的写法

2.使用shared_ptr智能指针,  这个好处就不用多少了,  可以自动管理内存

3.使用lambda表达式配合std::call_once 函数,实现线程安全,并且让代码开起来更加清晰

<std::call_once可以保证多个线程对该函数只调用一次。也可用在解决线程安全的单例模式>

以下是调用方法:

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
#include <iostream> #include "design/Singleton.hpp" class MyClass { public: MyClass() :m_value(10) {} MyClass(int value) :m_value(value) {} ~MyClass() { std::cout << "xigou" << std::endl; } void destory() { std::cout << "destory" << std::endl; } void print() { std::cout << m_value << std::endl; } private: int m_value; }; int main(int argc, char const *argv[]) { auto sg1 = Singleton<MyClass>::getInstance(); sg1->print(); auto sg2 = Singleton<MyClass>::getInstance(); sg2->print(); return 0; }

例子非常简单,  就是写一个类,  之后调用。   大家可以调试下,看看调用流程,这个不做过多解释

 

以下是CMakelists.txt

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(desgin_test) if (WIN32) message(STATUS "sys is windows") elseif (APPLE) message(STATUS "sys is Apple systems.") elseif (UNIX) message(STATUS "sys is UNIX-like OS's.") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++14") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g -Wall -pthread") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lthread") endif() aux_source_directory(. src_lists) add_executable(${PROJECT_NAME} ${src_lists})

好吧,  到此结束。

最后

以上就是欢喜酒窝最近收集整理的关于使用C++ 11实现单例模式的全部内容,更多相关使用C++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部