single.h:
#ifndef SINGLE_H
#define SINGLE_H
class single
{
private:
static single *p;
protected:
single();
public:
static single *makesignle();
static void releasesingle();
};
#endif // SINGLE_H
single.cpp:
#include "single.h"
#include <iostream>
single *single::p=NULL;
single::single()
{
}
single *single::makesignle()
{
if(p==NULL)
p=new single;//如果p为空,就实例化d对象
return p;
}
void single::releasesingle()
{
delete p;
}
main.cpp:
#include <iostream>
#include "single.h"
using namespace std;
//实例化单例出来
int main()
{
single *p=single::makesignle();
single *p1=single::makesignle();
single::releasesingle();
cout << "Hello World!" << endl;
return 0;
}