Re: [問題] 請問pointer to reference有什麼好處呢?
看板C_and_CPP (C/C++)作者walm20 (New CygnusX)時間16年前 (2009/03/27 19:21)推噓0(0推 0噓 0→)留言0則, 0人參與討論串1/2 (看更多)
一個class method return ref到member
可以給外界來存取 (l-value)
在此return 一個ref to ptr也是一樣的意思
※ 引述《allstarschh (allstars)》之銘言:
: 我在看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
--
我的露天拍賣:
http://tinyurl.com/6z4rw7
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.118.126.177
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章