[問題] singleton遇到一個瓶頸
請教各位一個情況
例如
class Foo
{
void intit(){ try{.new 那些instance;..} catch{} }
Foo()
{
intit();
}
這邊宣告好多指標 等待在init作new的動作
static Foo * m_Inst;
public:
static Foo* GetInstance()
{ if(m_Inst == null)
m_Inst = new Foo();
return m_Inst;
}
};
intit(){ 做到一半可能有exception 我有catch 導致 接下來的code都沒跑
member沒 new完全
雖說這樣 我constructor還是能夠成功做完
但其實我這instance半殘
我return 也回去了 對方就拿到一個半殘的instance...
我現在想不到該如何有效的回報給外面的使用者
我寧可出現問題我Return null給他也不要他拿到後用到某method 存取錯誤
但我想到的辦法變成說需要有一個static bool flag = init();
回報有沒有錯誤
然後雖說m_Inst new完有東西了
我在check 這flag
若失敗則return null
我這case 是不是設計不良?還是怎樣導致
我用flag去解 又覺得不太正確
請教各位
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.147.15.66
※ 編輯: QQ29 來自: 122.147.15.66 (11/25 21:15)
→
11/25 21:48, , 1F
11/25 21:48, 1F
→
11/25 21:50, , 2F
11/25 21:50, 2F
→
11/25 21:53, , 3F
11/25 21:53, 3F
→
11/25 21:59, , 4F
11/25 21:59, 4F
→
11/25 22:04, , 5F
11/25 22:04, 5F
→
11/25 22:06, , 6F
11/25 22:06, 6F
→
11/25 22:07, , 7F
11/25 22:07, 7F
→
11/25 22:52, , 8F
11/25 22:52, 8F
→
11/25 22:53, , 9F
11/25 22:53, 9F
→
11/25 22:53, , 10F
11/25 22:53, 10F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章