直接上代码
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
template <typename T>
class CSingleton
{
public:
static T *instance()
{
static T *_instance = NULL;
if (_instance == NULL)
_instance = new T();
return _instance;
}
protected:
CSingleton() {}
CSingleton(const CSingleton&);
CSingleton& operator =(const CSingleton&);
};
#endif // _SINGLETON_H_
#include <QtCore>
#include "singleton.h"
class CFactory : public CSingleton<CFactory>
{
public:
void setData(const QString &data) { m_data = data; }
QString data() { return m_data; }
public:
QString m_data;
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
CFactory::instance()->setData("11111");
QString temp = CFactory::instance()->data();
return app.exec();
}