[問題] 請問reference to pointer有什麼好處呢?
我在看WebKit的code時
發現某段code有用reference to pointer
bool JSGlobalData::sharedInstanceExists()
{
return sharedInstanceInternal();
}
JSGlobalData& JSGlobalData::sharedInstance()
{
JSGlobalData*& instance = sharedInstanceInternal();
if (!instance) {
instance = new JSGlobalData(true);
}
return *instance;
}
JSGlobalData*& JSGlobalData::sharedInstanceInternal()
{
ASSERT(JSLock::currentThreadIsHoldingLock());
static JSGlobalData* sharedInstance;
return sharedInstance;
}
請問在中間那個function sharedInstance
為什麼不把code搬到 sharedInstanceInternal就好了
這樣就不用用到 reference to pointer了
因為用reference to pointer的話
有一邊free/delete 的話 另一個reference是不是就會有dangling refernece了
所以想來請教reference to pointer的好處 thanks
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.67.242.96
推
03/27 18:46, , 1F
03/27 18:46, 1F
→
03/27 18:47, , 2F
03/27 18:47, 2F
→
03/27 19:07, , 3F
03/27 19:07, 3F
→
03/27 19:59, , 4F
03/27 19:59, 4F
→
03/27 22:49, , 5F
03/27 22:49, 5F
→
03/28 00:26, , 6F
03/28 00:26, 6F
→
03/28 00:27, , 7F
03/28 00:27, 7F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章