簡單的單例實現

//////test.h

class test
{
public:
	~test(void);
	static test* GetInstance();
	int Geta();
private:
	static test m_instance;
	test(void);
	int a;
};

//////test.cpp

test::test( void )
{
	a = 10;
	printf("create test!\n");
}

test::~test( void )
{
	printf("destroy test!\n");
}

test* test::GetInstance()
{
	return &m_instance;
}

int test::Geta()
{
	return a;
}

test test::m_instance;

void main()
{

	printf("test a:%d\n",test::GetInstance()->Geta());
	getchar();

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章