创建:
#include <iostream>
using namespace std;
class only
{
private:
only(){} //构造函数是私有的,这样就不能在其它地方创建该实例
static only *p; //定义一个唯一指向实例的静态指针,并且是私有的。
static int b;
public:
static only* GetInstance() //定义一个公有函数,可以获取这个唯一的实例,并且在需要时创建该实例。
{
if(p == NULL) //判断是否第一次调用
p = new only;
return p;
}
static void show()
{
cout << b << endl;
}
};
int only::b=66; //static定义的数据成员必须在类外初始化,因为它是整个类的一部分,而不是属于某个对象。
only* only::p=NULL;
int main()
{
only *a1=only::GetInstance();
cout << a1 << endl;
a1->show();
only *a2=only::GetInstance();
cout << a2 << endl;
a2->show();
return 0;
}
安全释放:
#include <iostream>
using namespace std;
class only
{
private:
only() {} //构造函数是私有的
static only *p;
static int b;
class cGarbo
{
public:
~cGarbo()
{
if( only::p )
{
delete only::p;
cout << "delete only success " << endl;
}
}
};
static cGarbo Garbo;
public:
static only* new_inst()
{
if(p == NULL) //判断是否第一次调用
p = new only;
return p;
}
static void show()
{
cout << b << endl;
}
};
int only::b=66;
only* only::p=NULL;
only::cGarbo only::Garbo;
int main()
{
only *a1=only::new_inst();
cout << a1 << endl;
a1->show();
only *a2=only::new_inst();
cout << a2 << endl;
a2->show();
return 0;
}