Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1084243
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: C/C++

2010-03-18 10:28:36

#include

using namespace std;

class Temp {
        public:
                Temp()  { cout << "constructor" << endl; }
                ~Temp() { cout << "destructor" << endl;  }

                static Temp * getInstance();
                static void putInstance();
        private:
                static Temp *instance;
};

Temp * Temp::getInstance()
{
        cout << "getInstance" << endl;
        if (!instance)
                instance = new Temp();

        return instance;
}

void Temp::putInstance()
{
        cout << "putInstance" << endl;
        if (instance) {
                delete instance;
                instance = NULL;
        }
}

Temp * Temp::instance = NULL;

int main()
{
        Temp *p;        /* don't invoke constructor */

        p = Temp::getInstance();

        Temp::putInstance();

        return 0;
}

阅读(1052) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-03-18 10:31:03

#include using namespace std; class Temp { public: static Temp * getInstance(); static void putInstance(); private: Temp() { cout << "constructor" << endl; } ~Temp() { cout << "destructor" << endl; } static Temp *instance; }; Temp * Temp::getInstance() { cout << "getInstance" << endl; if (!instance) instance = new Temp(); r