template + single instance

来源:互联网 发布:oracle 分页优化 编辑:程序博客网 时间:2024/05/17 07:18
#include <iostream>#include <cstring>using namespace std;template<class T>class Sing{    public:    Sing();    virtual ~Sing();    static T* getInstance();    static void destory();    private:       static T* instance;};template<class T>T* Sing<T> :: instance = NULL;template<class T>T* Sing<T> :: getInstance(){    if(NULL == instance)    {        instance = new T();    }    return instance;}template<class T>void Sing<T>::destory(){    if(NULL != instance)    {        delete instance;        instance = NULL;    }};class TestSing{    public:        TestSing()        {                str = Sing<char>::getInstance();                if(str != NULL)                {                     cout << "single test is ok"<<endl;                }        }       virtual ~TestSing()        {                Sing<char>::destory();                if(str == NULL)                {                    cout << "single destory is ok"<< endl;                }        }    private:        char* str;};int main(int argc, char* argv[]){    TestSing t;    return 0;}

原创粉丝点击