Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

设计一个类,该类不能被继承,且只能实例化3次

$
0
0
#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");
}

作者:sunmeng_Alex 发表于2013-5-3 19:53:20 原文链接
阅读:33 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>