c++单例模式
来源:互联网 发布:日系护肤品知乎 编辑:程序博客网 时间:2024/05/17 08:44
1.基本实现
class Singleton{public: static Singleton *Instance() { if (!spInstance) { spInstance = new Singleton; } return spInstance; }protected: Singleton() { } ~Singleton() { if (spInstance) { delete spInstance; } }private: static Singleton *spInstance;};Singleton *Singleton::spInstance = nullptr;
2.单例模板
singleton.h
#ifndef SINGLETON_H#define SINGLETON_Htemplate<typename T>class Singleton{public: static T &Instance() { static T sInstance; return sInstance; }private: Singleton() { } Singleton(const Singleton &); Singleton &operator=(const Singleton &);};#endif
main.cpp
#include <iostream>#include "singleton.h"using namespace std;class Example{public: Example() { } ~Example() { } void Print();};void Example::Print(){ cout << "Example.Print()" << endl;}int main(){ Singleton<Example>::Instance().Print();}
3.改进的单例模板
singleton.h
#ifndef SINGLETON_H#define SINGLETON_Htemplate<typename T>class Singleton{public: static T &Instance() { static T sInstance; return sInstance; }protected: Singleton() { } virtual ~Singleton() { }private: Singleton(const Singleton &); Singleton &operator=(const Singleton &);};#endif
main.cpp
#include <iostream>#include "singleton.h"using namespace std;class Example : public Singleton<Example>{ friend class Singleton<Example>;public: void Print();private: Example() { } virtual ~Example() { }};void Example::Print(){ cout << "Example.Print()" << endl;}int main(){ Example::Instance().Print();}
4.带参数的单例模板
singleton.h
#ifndef SINGLETON_H#define SINGLETON_H#include <utility>template<typename T>class Singleton{public: template<typename... Args> static void CreateInstance(Args&&... args) { spInstance = new T(std::forward<Args>(args)...); } static T *Instance() { return spInstance; } static void DestroyInstance() { if (spInstance) { delete spInstance; spInstance = nullptr; } }protected: Singleton() { } virtual ~Singleton() { }private: Singleton(const Singleton &); Singleton &operator=(const Singleton &);private: static T *spInstance;};template<typename T>T *Singleton<T>::spInstance = nullptr;#endif
main.cpp
#include <iostream>#include "singleton.h"using namespace std;class Example : public Singleton<Example>{ friend class Singleton<Example>;public: void Print();private: Example(int i) { member = i; } virtual ~Example() { }private: int member;};void Example::Print(){ cout << "Example.Print(), member = " << member << endl;}int main(){ Example::CreateInstance(3); Example::Instance()->Print(); Example::DestroyInstance();}
0 0
- C#单例模式
- C#-单例模式
- 单例模式(C++)
- 【C++】单例模式
- [C++]单例模式
- 单例模式(C++)
- C ++的单例模式
- 【C++】Chapter17:单例模式
- Object-c单例模式
- Objectove-c单例模式
- Objective C--单例模式
- Objective C--单例模式
- Objective C 单例模式
- Objective C--单例模式
- 单例模式(C#)
- 单例模式(C#)
- object-c 单例模式
- c++-单例模式Singleton
- 禁掉谷歌浏览器(google)的记住密码弹出框
- 商城之商品分享
- hdu 2955(01背包变形)
- python爬虫代码-CSDN博客下载
- 【改自CC_CHSEQ22】【JZOJ4807】破解 题解
- c++单例模式
- 八数码的八境界
- The Most Important Algorithms (in CS and Math)
- 碰运气(YourLucky)
- Java内部类(成员内部类、局部内部类、静态内部类、匿名内部类)
- 记又一次unhappy考试(10.6)
- awk中可以使用system来执行复杂的shell命令
- 数组简单介绍
- 关于eSIM的一些简单介绍