2012-07-25 89 views
0
scoped_refptr<any_class> a_scoped_refptr; 
const scoped_refptr<any_class> something = a_scoped_refptr; // Compile error. 

如果它的常量比我們無法修改ref_counter,但我想暗示指針指向的內容不應該改變,我該怎麼辦?是否有意義定義const scope_refptr()?

回答

1

兩件事情:

首先,你沒有提到的編譯器錯誤,但我猜這是一個簡單的語法錯誤,因爲你沒有爲scoped_refptr指定變量名。其次,如果你想要一個指向一個const對象的指針,指定它作爲指針類型。所以請嘗試:

+0

謝謝,是scoped_refptr myPointer = a_scoped_refptr;與scoped_refptr myPointer = a_scoped_refptr;有什麼不同? – 2012-07-25 20:22:19

+0

不;一樣的。 'const mytype'只是一個C++方便的語法,用於更「適當」的'mytype const'。 – tenfour 2012-07-25 20:31:13