设计模式-创建型-单件
原创
©著作权归作者所有:来自51CTO博客作者shixin_0125的原创作品,请联系作者获取转载授权,否则将追究法律责任
#pragma once
#ifndef SINGLETON_H
#define SINGLETON_H
#include <memory>
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 <iostream>
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!\n";
}
#include "stdafx.h"
#include "singleton_impl.h"
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
Singleton::GetInstancePtr()->Test();
Singleton::GetInstance().Test();
system("pause");
return 0;
}