-
Notifications
You must be signed in to change notification settings - Fork 1
/
singleton.h
40 lines (39 loc) · 1.39 KB
/
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
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
/************************************************************************/
/* Meyers Singleton, 退出时释放资源 */
/************************************************************************/
template <class SingletonClass >
class MeyersSingleton{
public:
static SingletonClass * Instance(){
static SingletonClass instance;
return &instance;
}
SingletonClass* operator ->() { return Instance(); }
const SingletonClass* operator ->() const { return Instance(); }
private:
MeyersSingleton(){ }
~MeyersSingleton(){ }
};
/************************************************************************/
/* 普通的Singleton, 退出时不释放资源 */
/************************************************************************/
// 利用构造函数私有化,使本类只能有一个对象实体 --解读注释
template <class SingletonClass >
class NormalSingleton{
public:
static SingletonClass * Instance(){
static SingletonClass* instance = 0;
if( !instance ){
instance = new SingletonClass;
}
return instance;
}
SingletonClass* operator ->() { return Instance(); }
const SingletonClass* operator ->() const { return Instance(); }
private:
NormalSingleton(){ }
~NormalSingleton(){ }
};
#endif