关闭

单例

449人阅读 评论(0) 收藏 举报
分类:
#pragma once
#include <iostream>
using namespace std;

class SingtonTest
{
public:
	static SingtonTest* getInstance();
	static bool destroy();
private:
	static SingtonTest* instance;
	int data;
private:
	virtual bool init();
	SingtonTest(void);
	virtual ~SingtonTest(void);
	SingtonTest(const SingtonTest& st);
	SingtonTest& operator=(const SingtonTest& st);
public:
	static int n;
};

#include "SingtonTest.h"


SingtonTest* SingtonTest::instance = NULL;

SingtonTest::SingtonTest(void)
{
	static int count = 1;

	cout<<"create instatnce: "<<count<<endl;
    count++;

}
SingtonTest* SingtonTest:: getInstance()
{
	if(!instance)
	{
		instance = new SingtonTest();
		instance->init();
	}
	return instance;
}
bool SingtonTest::init()
{
	data = 1;
	return true;
}
SingtonTest::SingtonTest(const SingtonTest& st)
{

}
SingtonTest& SingtonTest::operator=(const SingtonTest& st)
{
	return *this;
}
bool SingtonTest::destroy()
{
	if (instance)
	{
		delete instance;
		instance = NULL;
		return true;
	}
	return false;
}

SingtonTest::~SingtonTest(void)
{
	cout<<"delete"<<endl;
}

#include "SingtonTest.h"


int main()
{
	
	SingtonTest* pSington1 = SingtonTest::getInstance();
	SingtonTest* pSington2 = SingtonTest::getInstance();
	SingtonTest* pSington3 = SingtonTest::getInstance();

	SingtonTest::destroy();
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:576362次
    • 积分:7884
    • 等级:
    • 排名:第2624名
    • 原创:215篇
    • 转载:107篇
    • 译文:11篇
    • 评论:55条
    最新评论