#include "stdafx.h"
#include <iostream>
using namespace std;
class Singleton
{
private:
Singleton();
~Singleton(){};
int var;
public:
static Singleton *instance;
static Singleton *Createinstance()
{
if(instance == NULL)
{
instance = new Singleton();
static Del del;
}
return instance;
}
static void Destroy();
void SetVar(int value);
int GetVar();
class Del
{
public:
~Del()//垃圾回收
{
if(Singleton::instance!=NULL)
{
cout<<"11111111"<<endl;
delete Singleton::instance;
}
}
};
static Del mDel;
};
Singleton * Singleton::instance = NULL;
Singleton::Del Singleton::mDel;
void Singleton::Destroy()
{
delete instance;
}
int Singleton::GetVar()
{
return var;
}
void Singleton::SetVar( int value )
{
this->var = value;
}
Singleton::Singleton()
{
this->var = 10;
}
int main()
{
cout<<Singleton::Createinstance()->GetVar();
Singleton::Createinstance()->SetVar(100);
cout<<Singleton::Createinstance()->GetVar();
return 0;
}