#pragma once
#ifndef SINGLETON_H
#define SINGLETON_H
#include
using namespace std;
class Singleton
{
private:
Singleton(){};
public:
// 静态成员函数,提供全局访问的接口
static Singleton* GetInstancePtr();
static Singleton GetInstance();
void Test();
protected:
// 静态成员变量,提供全局惟一的一个实例
static Singleton* m_pStatic;
};
#endif
#include "StdAfx.h"
#include "singleton_impl.h"
#include
// 类的静态成员变量要在类体外进行定义
Singleton* Singleton::m_pStatic = NULL;
Singleton* Singleton::GetInstancePtr()
{
if (NULL == m_pStatic)
{
m_pStatic = new Singleton();
}
return m_pStatic;
}
Singleton Singleton::GetInstance()
{
return *GetInstancePtr();
}
void Singleton::Test()
{
std::cout Test();
Singleton::GetInstance().Test();
system("pause");
return 0;
}