mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
34 lines
526 B
Markdown
34 lines
526 B
Markdown
# 单例模式
|
|
|
|
```cpp
|
|
// 懒汉式单例模式
|
|
class Singleton
|
|
{
|
|
private:
|
|
Singleton() { }
|
|
static Singleton * pInstance;
|
|
public:
|
|
static Singleton * GetInstance()
|
|
{
|
|
if (pInstance == nullptr)
|
|
pInstance = new Singleton();
|
|
return pInstance;
|
|
}
|
|
};
|
|
|
|
// 线程安全的单例模式
|
|
class Singleton
|
|
{
|
|
private:
|
|
Singleton() { }
|
|
~Singleton() { }
|
|
Singleton(const Singleton &);
|
|
Singleton & operator = (const Singleton &);
|
|
public:
|
|
static Singleton & GetInstance()
|
|
{
|
|
static Singleton instance;
|
|
return instance;
|
|
}
|
|
};
|
|
``` |