Code Bye

C++ SingleTon问题,初次学习,麻烦指点下哪里有问题,O(∩_∩)O谢谢

class SingleTonDemo
{
private:
	static SingleTonDemo *pInstance;
	SingleTonDemo(){
		if (!pInstance)
		{
			pInstance = new SingleTonDemo();
		}
	}
	~SingleTonDemo(){
		print("Destructor Invoked");
	}
public:
	static SingleTonDemo* getInstace(){
		if (!pInstance)
		{
			pInstance = new SingleTonDemo();
		}
		return pInstance;
	}
	static void Destroy(SingleTonDemo* pInstance){
		if (pInstance)
		{
			delete pInstance;
			pInstance = NULL;
		}
	}
	void SayHello(){
		print("Hello~");
	}
};
void main(){
	SingleTonDemo  *demo = SingleTonDemo::getInstace();
}

第一次发帖子,有什么不对的还请多包涵(*^__^*) 嘻嘻……

解决方案

40

SingleTonDemo 的构造函数里面又在调用 SingleTonDemo 的构造函数, 死循环

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C++ SingleTon问题,初次学习,麻烦指点下哪里有问题,O(∩_∩)O谢谢