您当前的位置: 首页 > 

phymat.nico

暂无认证

  • 1浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

设计模式-创建型-单件

phymat.nico 发布时间:2017-10-07 15:43:09 ,浏览量:1

#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;
}

关注
打赏
1659628745
查看更多评论
立即登录/注册

微信扫码登录

0.0444s