首页 > 其他 > 详细

Singleton Pattern(单例模式)

时间:2015-05-03 13:22:50      阅读:231      评论:0      收藏:0      [点我收藏+]
<pre name="code" class="cpp">/*Singleton.h*/
#ifndef SINGLETON_H
#define SINGLETON_H

class Singleton
{
public:
	static Singleton *Instance();
protected:
	Singleton();
private:
	static Singleton *instance_;
};
#endif


<pre name="code" class="cpp">/*Singleton.cpp*/
#include "Singleton.h"
#include <iostream>

Singleton *Singleton::instance_=0;

Singleton::Singleton()
{
	std::cout<<"Singleton..."<<std::endl;
}

Singleton *Singleton::Instance()
{
	if(instance_==0)
	{
		instance_=new Singleton();
	}
	return instance_;
}

/*main.cpp*/
#include "Singleton.h"

int main()
{
	Singleton *sgn=Singleton::Instance();
	return 0;
}

Singleton Pattern(单例模式)

原文:http://blog.csdn.net/tlzhatao/article/details/45458027

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!