单例模式( Singleton)

来源:互联网 发布:2017年网络创业好项目 编辑:程序博客网 时间:2024/06/05 20:48
#ifndef _SINGLETON_H#define _SINGLETON_H#include "stdafx.h"class Singleton{public:static Singleton * instance();private:static Singleton * _instance;};#endif

#include "stdafx.h"#include "singleton.h"Singleton *Singleton::_instance = 0;Singleton* Singleton::instance(){if (_instance == 0){_instance = new Singleton;return _instance;}return _instance;}

// Singleton.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "singleton.h"#include <Windows.h>int _tmain(int argc, _TCHAR* argv[]){Singleton * sgn = Singleton::instance();Sleep(1000);printf("%d", sgn);Singleton * svn = Singleton::instance();Sleep(1000);printf("%d", svn);Sleep(200);while(1);return 0;}

0 0
原创粉丝点击