单例模式 模板类

#ifndef SINGLETON_H
#define SINGLETON_H

#include <QObject>

template < typename T >
class Singleton
{

public:
    static T* getInstance();

private:
    static T* c_instance;
};

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

template < typename T >
T* Singleton<T>::getInstance()
{
    if(nullptr == c_instance)
    {
        c_instance = new T();
    }

    return c_instance;
}

#endif // SINGLETON_H

 

上一篇:c: \Users\ATFWUS\AppData\Local(Temp\tomcat-docbase.613353598438846626.8802\articleImage\2828/


下一篇:四,创建者模式 -->单例模式