独立件简单的singleton用c++实现

#include <iostream>
using namespace std;
class Singleton
{
public:
 static Singleton *Getinstance()
 {
 if(p!=NULL)
 {
  return p;
 }
 else
 {
  p=new Singleton;
  return p;
 }
 }
 static Singleton* Free()
 {
  if(p!=NULL)
  {
   delete p;
   p=NULL;
   return NULL;
  }
  else
   return NULL;
 }
 static ShowInstance()
 {
  cout<<p<<endl;
 }
private:
 static Singleton *p;
 Singleton(){};
 ~Singleton(){};
};

Singleton *Singleton::p=NULL;

int main()
{
 class Singleton *p=Singleton::Getinstance();
 Singleton::ShowInstance();
 cout<<p<<endl;
 p=Singleton::Free();
 cout<<p<<endl;
 Singleton::ShowInstance();
 return 0;
}

Leave a Reply

Your email address will not be published.