0
点赞
收藏
分享

微信扫一扫

2021-06-16

墨香子儿 2022-04-13 阅读 31
单例模式
template<class T>
    class Singleton
    {
    public:
        Singleton()
        {
            LordAssert(!ms_pSingleton);
            ms_pSingleton = static_cast<T*>(this);
        }

        ~Singleton()
        {
            LordAssert(ms_pSingleton);
            ms_pSingleton = NULL;
        }

    private:
        /** \brief Explicit private copy constructor. This is a forbidden operation.*/
        Singleton(const Singleton<T>&);

        /** \brief Private operator= . This is a forbidden operation. */
        Singleton& operator = (const Singleton<T>&);

    public:
        static T* Instance()
        {
            return ms_pSingleton;
        }

    protected:
        static T* ms_pSingleton;
    };

    template<class T>
    T* Singleton<T>::ms_pSingleton = NULL;
举报

相关推荐

0 条评论