A singleton template class

Back to topics list To post a new topic, please log in or register
avatar
20
niquedegraaff 2016.05.07 01:41
 

Since build 900 allows us to make parametrized classes I was trying to find a way of creating a Singleton base class. There is nearly no information about Template classes so
i tried to mimic c++ kind of template classes.

template <typename T>
class Singleton
{
private:
    static  T*              m_instance;
            Singleton(Singleton const&);
            Singleton&      operator = (Singleton const&);


protected:               
            void            Singleton();
            void           ~Singleton();
public:
    static  T*              GetInstance() {
                                if(m_instance == NULL)
                                    m_instance = new T;
                                return m_instance;
                            }

};

template <typename T> 
T* Singleton<T>::m_instance=NULL;

  

Of course, this doesn't work. But I am missing anything about it in the docs so.. did anyone else created a similar Singleton template class? Can you share it with me / the world? 
Thank you

avatar
12992
WHRoeder 2016.05.07 14:57 #
 
  1. You have to allocate an actual one:
    ActualType* Singleton<ActualType>::m_instance=NULL;
  2. And you have to remember to delete it in OnDeinit.
  3. I use this pattern
    #define DISALLOW_COPY_AND_ASSIGN(T) void T(const T&); void operator=(const T&)
    class Noncopyable{
     protected:                                                          // Abstract
       void                 Noncopyable(void){}                             //[Ctor]
     private:   DISALLOW_COPY_AND_ASSIGN(Noncopyable);
    };
    class Singleton : Noncopyable
    {
    private:
        static  T*              m_instance;
    //          Singleton(Singleton const&);
    //          Singleton&      operator = (Singleton const&);
    protected:
    :

Back to topics list  

To add comments, please log in or register