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 關注
- 393 瀏覽
添加回答
舉報
