先在頭文件聲明:
聲明一個COemInstancer的 _this指針:
static?COemInstance* _this;
.然后在文件外層這樣寫:
#define CXXModule COemInstance::instance()
#define ExecuteCommand(ClassName,RunCommand) class Tempclass##ClassName\
{public:Tempclass##ClassName(){RunCommand;}};\
?Tempclass##ClassName g_Tempclass##ClassName;
cpp:這樣寫:
先全局聲明:
COemInstance * COemInstance::_this = nullptr;
ExecuteCommand(COemInstance, COemInstance::instance())
調用的時候就可以
?CXXModule--->xxx
多一層ExecuteCommand 的好處是確保百分百加載,然后在多線程中確保安全加載
COemInstance* COemInstance::instance()
{
????if (!_this)
????{
????????if (!_bDestoryed_)
????????{
????????????_this = new COemInstance();
????????????//_this->Initialization();
????????????atexit(&COemInstance::destory);
????????}
????}
????return _this;
}
void COemInstance::destory()
{
????if (_this)
????{
????????delete _this;
????????_this = NULL;
????????_bDestoryed_ = true;
????}
}
?