单例继承模式

#include 

using namespace std;

template <typename T>
class Singleton
{
public:
    static T&   GetSingleton(void);
    static T*   GetSingletonPtr(void);
protected:
    static T* m_pSingleton;
};

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

template <typename T>
T& Singleton::GetSingleton(void)
{
        m_pSingleton = new T;
    return *m_pSingleton;
}

template <typename T>
T* Singleton::GetSingletonPtr(void)
{
        m_pSingleton = new T;
    return m_pSingleton;
}

class CMsgManager : public Singleton
{
public:
    virtual bool Initialize();
};

bool CMsgManager::Initialize()
{

    cout<<"Bool CMsgManager::Initialize()"<return 0;
}

int main(int argc, char** argv)
{
    cout<<"Enter StartPreThread()"<do 
    {
        if (!CMsgManager::GetSingletonPtr()->Initialize())
        {
            cout<<"CMsgManager Initialize failed"<break;
        }

    } while (0);

    cout<<"Exit StartPreThread()"<return 0;
}

你可能感兴趣的:(其他)