码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 唯一实例类

时间:2015-03-20 01:13:59      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

通过Instance 来产生一个实例指针。

使用:

class ServerSessionFactory: public Singleton<ServerSessionFactory>

// Singleton.h: 唯一实例
// 通过调用 Instance 来初始化得到唯一实例
/////////////////////////////////////////////////////////////////////////////

#ifndef _SINGLETON_H
#define _SINGLETON_H


namespace util
{
template <class T>
class Singleton
{
public:
    ~Singleton(){}
    
    static T* Instance()
    {
        if(ms_pInstance == 0)
        {
            ms_pInstance = new T;
        }
        return ms_pInstance;
    }
    
    static void DestroyInstance()
    {
        if(ms_pInstance)
        {
            delete ms_pInstance;
            ms_pInstance = NULL;
        }
    }
    
private:
    static T* ms_pInstance;
};

template <class T>
T* Singleton<T>::ms_pInstance = 0;    
}

#endif

 

C++ 唯一实例类

标签:

原文地址:http://www.cnblogs.com/sylar-liang/p/4352281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!