#include<iostream>
class finalclass
{
public: static int count;
public:
static finalclass *getinstance()
{
if(count <= 0)
return NULL;
count--;
return new finalclass;
}
static void setcount(int n)
{
count = n;
}
/* static void deleteinstance(finalclass *pinstance)
{
delete pinstance;
pinstance = 0;
}
*/
private:
finalclass(){}
~finalclass(){}
};
int finalclass::count = 0;
int main()
{
finalclass::setcount(3);
finalclass *f1 = finalclass::getinstance();
finalclass *f2 = finalclass::getinstance();
finalclass *f3 = finalclass::getinstance();
if(f3 == NULL)
printf("f3 NULL\n");
else
printf("f3 NOT NULL\n");
finalclass *f4 = finalclass::getinstance();
if(f4 == NULL)
printf("f4 NULL\n");
finalclass *f5 = finalclass::getinstance();
if(f5 == NULL)
printf("f5 NULL\n");
}
设计一个类,该类不能被继承,且只能实例化3次
最新推荐文章于 2019-01-30 13:43:16 发布