设计模式单例模式
Posted zhao111222333444
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式单例模式相关的知识,希望对你有一定的参考价值。
#include<iostream>
#include<mutex>
using namespace std;
class single
public:
static single* get()
return &s;
private:
single();
single(single &a) = delete;
single& operator=(single &a) = delete;
static single s;
;
single single::s;
class single2
public:
static single2* get()
if (_ptr == nullptr)
_mutex.lock();
if (_ptr == nullptr)
_ptr = new single2;
_mutex.unlock();
return _ptr;
private:
single2();
single2(single2 &a) = delete;
single2& operator=(single2 &a) = delete;
static single2* _ptr;
static mutex _mutex;
;
single2* single2::_ptr = nullptr;
mutex single2::_mutex;
以上是关于设计模式单例模式的主要内容,如果未能解决你的问题,请参考以下文章