class Singleton
{
private:
static Singleton* m_instance;
Singleton(){}
public:
static Singleton* getInstance();
};
Singleton* Singleton::getInstance()
{
if(NULL == m_instance)
{
Lock();
//借用其它类来实现,如boost
if(NULL == m_instance)
{
m_instance = new Singleton;
}
UnLock();
}
return m_instance;
}
class SingletonInside
{
private:
SingletonInside(){}
public:
static SingletonInside* getInstance()
{
Lock();
// not needed after C++0x
static SingletonInside instance;
UnLock();
// not needed after C++0x
return instance;
}
};
class SingletonStatic
{
private:
static const SingletonStatic* m_instance;
SingletonStatic(){}
public:
static const SingletonStatic* getInstance()
{
return m_instance;
}
};
//外部初始化 before invoke main
const SingletonStatic* SingletonStatic::m_instance = new SingletonStatic;
class QMManager
{
public:
static QMManager &instance()
{
static QMManager instance_;
return instance_;
}
}
static QMManager &instance()
{
static bool constructed = false;
static uninitialized QMManager instance_;
if (!constructed) {
constructed = true;
new(&s) QMManager; //construct it
}
return instance_;
}
static QMManager &instance()
{
Lock(); //锁自己实现
static QMManager instance_;
UnLock();
return instance_;
}
class QMManager
{
protected:
static QMManager instance_;
QMManager();
~QMManager(){};
public:
static QMManager *instance()
{
return &instance_;
}
void do_something();
};
QMManager QMManager::instance_; //外部初始化
//.h
class QMManager
{
protected:
static QMManager instance_;
QMManager();
~QMManager(){};
public:
static QMManager *instance()
{
return &instance_;
}
};
class QMSqlite
{
protected:
static QMSqlite instance_;
QMSqlite();
~QMSqlite(){};
public:
static QMSqlite *instance()
{
return &instance_;
}
void do_something();
};
QMManager QMManager::instance_;
QMSqlite QMSqlite::instance_;
//.cpp
QMManager::QMManager()
{
printf("QMManager constructor\n");
QMSqlite::instance()->do_something();
}
QMSqlite::QMSqlite()
{
printf("QMSqlite constructor\n");
}
void QMSqlite::do_something()
{
printf("QMSqlite do_something\n");
}
QMManager constructor QMSqlite do_something QMSqlite constructor
//.h
class QMManager
{
protected:
struct object_creator
{
object_creator()
{
QMManager::instance();
}
inline void do_nothing() const {}
};
static object_creator create_object_;
QMManager();
~QMManager(){};
public:
static QMManager *instance()
{
static QMManager instance;
return &instance;
}
};
QMManager::object_creator QMManager::create_object_;
class QMSqlite
{
protected:
QMSqlite();
~QMSqlite(){};
struct object_creator
{
object_creator()
{
QMSqlite::instance();
}
inline void do_nothing() const {}
};
static object_creator create_object_;
public:
static QMSqlite *instance()
{
static QMSqlite instance;
return &instance;
}
void do_something();
};
QMManager::object_creator QMManager::create_object_;
QMSqlite::object_creator QMSqlite::create_object_;
QMManager constructor QMSqlite constructor QMSqlite do_something
template <typename T>
struct Singleton
{
struct object_creator
{
object_creator(){ Singleton<T>::instance(); }
inline void do_nothing()const {}
};
static object_creator create_object;
public:
typedef T object_type;
static object_type& instance()
{
static object_type obj;
//据说这个do_nothing是确保create_object构造函数被调用
//这跟模板的编译有关
create_object.do_nothing();
return obj;
}
};
template <typename T> typename Singleton<T>::object_creator Singleton<T>::create_object;
class QMManager
{
protected:
QMManager();
~QMManager(){};
friend class Singleton<QMManager>;
public:
void do_something(){};
};
int main()
{
Singleton<QMManager>::instance()->do_something();
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有