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

C++单例模板

时间:2015-01-13 15:50:06      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

#pragma once

namespace MyGame
{
    template<typename T>
    class Global
    {
    public:
        static void Create()
        {
            ASSERT(!s_instance);
            s_instance = new T;
        }

        static void Set(T* obj)
        {
            ASSERT(!s_instance);
            s_instance = obj;
        }

        static T& Get()
        {
            ASSERT(s_instance);
            return *s_instance;
        }

        static void Destroy()
        {
            ASSERT(s_instance);
            delete s_instance;
            s_instance = 0;
        }
    private:
        Global();
        Global(Global&);
        Global& operator =(Global&);

        static T* s_instance;
    };

    template<typename T>
    T* Global<T>::s_instance = 0;
}

 

C++单例模板

标签:

原文地址:http://www.cnblogs.com/shihui142857/p/4221430.html

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