单例模式 模板类

#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
原文地址:https://www.cnblogs.com/larkin-cn/p/14788777.html