close
shared_ptr是一種智慧指標(smart pointer),作用有如同指標,但會記錄有多少個shared_ptrs共同指向一個物件。這便是所謂的引用計數(reference counting),比如我們把只能指標賦值給另外一個物件,那麼物件多了一個智慧指標指向它,所以這個時候引用計數會增加一個,我們可以用shared_ptr.use_count()函式檢視這個智慧指標的引用計數
|
|
存取 std::shared_ptr 的原始指標 透過std::shared_ptr.get()可以取得原始指標,大概有兩種情況會使用到,一種是需要呼叫傳統的api,會需要傳遞原始指標,另一種是直接用原始指標,範例如下: |
|
ref:
https://www.itread01.com/content/1541464383.html
https://shengyu7697.github.io/blog/2019/11/21/std-shared-ptr/
全站熱搜