3 回答
TA贡献1804条经验 获得超2个赞
解决初始化顺序:
class A{ public: // Get the global instance abc static A& getInstance_abc() // return a reference { static A instance_abc; return instance_abc; }};
多线程问题:
§6.7[stmt.dcl]p4
如果在初始化变量时控件同时输入声明,则并发执行应等待初始化完成。
getInstance_XXX()
创作问题:
销毁问题:
class B{ public: static B& getInstance_Bglob; { static B instance_Bglob; return instance_Bglob;; } ~B() { A::getInstance_abc().doSomthing(); // The object abc is accessed from the destructor. // Potential problem. // You must guarantee that abc is destroyed after this object. // To guarantee this you must make sure it is constructed first. // To do this just access the object from the constructor. } B() { A::getInstance_abc(); // abc is now fully constructed. // This means it was constructed before this object. // This means it will be destroyed after this object. // This means it is safe to use from the destructor. }};
TA贡献1859条经验 获得超6个赞
_initterm
_initterm
_initterm
- 3 回答
- 0 关注
- 363 浏览
添加回答
举报