文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>c++ single instance

c++ single instance

时间:2010-03-18  来源:@sky

#include <iostream>

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;
}

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载