Singleton pattern: Create the only object in the class, and provide a interface to visit the object.
1
2
3
4
5
6
7
8
9
10
| //singleton.h
class CSingleton{
public:
static CSingleton& GetInstance();
~CSingleton();
private:
CSingleton();
struct Impl;
std::unique_ptr<Impl> impl;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| //singleton.cpp
struct CSingleton::Impl{
int mem1;
Impl(){ mem1 = 0; };
~Impl(){}
};
CSingleton::CSingleton(): impl(new Impl()){}
CSingleton::~CSingleton(){}
CSingleton& CSingleton::GetInstance(){
static CSingleton st;
return st;
}
|