65,208
社区成员
发帖
与我相关
我的任务
分享
#ifndef SINGLETON_H
#define SINGLETON_H
#include <iostream>
class Singleton
{
public:
static Singleton* GetInstance();
private:
static Singleton* instance;
Singleton();
~Singleton();
};
#endif
#include "Singleton.h"
Singleton* Singleton::instance = NULL;
Singleton* Singleton::GetInstance()
{
if(instance == NULL)
{
instance = new Singleton();
}
return instance;
}
Singleton::Singleton()
{
}
Singleton::~Singleton()
{
delete instance;
}
#include <iostream>
#include "Singleton.h"
using namespace std;
int main()
{
Singleton* a = Singleton::GetInstance();
Singleton* b = Singleton::GetInstance();
if(a == b)
cout<<"相等"<<endl;
system("pause");
return 0;
}
还有,为什么要把析构函数写成private 呢,求解释