Generic Singleton Base Class


출처 : http://vallista.tistory.com/entry/1-Singleton-Pattern-in-C



#pragma once

//http://vallista.tistory.com/entry/1-Singleton-Pattern-in-C

template < typename T >
class Singleton
{
protected:
    Singleton()
    {

    }
    virtual ~Singleton()
    {

    }

public:
    static T * GetInstance()
    {
        if (m_pInstance == nullptr)
        {
            m_pInstance = new T;
        }
        return m_pInstance;
    }

    static void DestoryInstance()
    {
        if (m_pInstance)
        {
            delete m_pInstance;
            m_pInstance = nullptr;
        }
    }

private:
    static T * m_pInstance;
};

template < typename T > T * Singleton<T>::m_pInstance;



간단 활용법

#include "Singleton.h" class Communicator : public Singleton<Communicator> { public: Communicator(); ~Communicator(); }



Yamecoder 야매코더_
C++(oF) 2017.02.24 11:04
Powerd by Tistory, designed by criuce
rss