曾經有個面試官問我所了解的智能指針,今天來總結一下^_^
C++ PRimer中提到4種指針 - shared_ptr - unique_ptr(auto_ptr) - weak_ptr
auto_ptr是C++ 98標準中的自動指針,unique_ptr是C++ 11標準中對應的升級版,但auto_ptr仍然是標準庫的一部分,auto_ptr具有unique_ptr的部分特性,但不是全部。
還有boost中的提到了6種智能指針 - scoped_ptr - scoped_array - shared_ptr - shared_array - weak_ptr - intrusive_ptr 其中的shared_ptr和weak_ptr已經被收錄到C++ 11標準中了。
shared_ptr:引用計數智能指針 unique_ptr(auto_ptr):所有權轉移智能指針 weak_ptr:shared_ptr的助手,peek一下shared_ptr管理的資源
scoped_ptr:所有權不轉讓智能指針 scoped_array:類似scoped_ptr,指向動態數組的智能指針 shared_array:類似shared_array,指向動態數組的智能指針 intrusive_ptr:侵入式的引用計數型指針
其詳細用法請查看C++ primer(第五版)的第12章動態內存 同時查看boost程序庫完全開發指南的第3章內存管理
新聞熱點
疑難解答
圖片精選