- class A
- {
- public:
- //this function is static !
- static A* instance(void)
- {
- if (n<3) { return new A; }
- else return (A*)0;
- }
- //this func is nont static !
- void print(void) const { cout<<n<<endl;}
- private:
- A(){ ++n;}
- static int n;
- };
- int A::n=0;//do not forget to init n
- int main( void )
- {
- A* p=NULL;
- for (int i=0;i<5;i++)
- {
- p=A::instance();
- if (p!=NULL)
- {
- p->print();
- }
- }
- return 0;
- }

被折叠的 条评论
为什么被折叠?



