#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 查看评论