Download MetaTrader 5
To add comments, please log in or register
Interested in earning money via MQL5 Cloud Network? Enter your profile!
niquedegraaff 2016.05.06 23: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
    static  T*              m_instance;
            Singleton(Singleton const&);
            Singleton&      operator = (Singleton const&);

            void            Singleton();
            void           ~Singleton();
    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

whroeder1 2016.05.07 12: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
        static  T*              m_instance;
    //          Singleton(Singleton const&);
    //          Singleton&      operator = (Singleton const&);

To add comments, please log in or register