C++之智能指针和普通指针单例模式两种实现
发布日期:2021-06-29 14:13:05 浏览次数:2 分类:技术文章

本文共 764 字,大约阅读时间需要 2 分钟。

1  问题

实现c++的单例模式,这里测试分别写了通过智能指针返回对象和普通返回指针

 

 

2  代码测试

include 
#include
#include
using namespace std;class Single{public: static Single& getInstance() { std::mutex mt; if (instance.get() == NULL) { mt.lock(); if (instance.get() == NULL) { instance.reset(new Single()); } mt.unlock(); } return *instance; }private: Single(){} ~Single(){} static std::auto_ptr
instance; friend class std::auto_ptr
; Single(const Single&); Single& operator= (const Single&);};std::auto_ptr
Single::instance;class Single1{public: static Single1* getInstance() { mutex mt; if (instance == NULL) { mt.lock(); if (in

转载地址:https://chenyu.blog.csdn.net/article/details/83834897 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Effective C++学习笔记之copy构造函数和default函数和copy赋值函数(operator=)
下一篇:C++编译之提示ld: can‘t open output file for writing: test1, errno=21 for architecture x86_64

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月13日 07时31分11秒