3 回答

TA貢獻1799條經驗 獲得超6個贊
共有所有權:shared_ptr
weak_ptr
weak_ptr
shared_ptr
shared_array
shared_ptr<std::vector<T> const>
.
intrusive_ptr
獨特所有權:scoped_ptr
std::unique_ptr
boost::scoped_ptr
boost::scoped_ptr
scoped_array
std::unique_ptr<T[]>
delete[]
delete
default_delete
std::unique_ptr<T[]>
operator[]
operator*
operator->
.
std::auto_ptr
§D.10 [depr.auto.ptr]
類模板 auto_ptr
不受歡迎。[ 注:類模板 unique_ptr
(20.7.1)提供了一個更好的解決方案。 -尾注 ]
無所有權:
shared_ptr
weak_ptr
shared_ptr
lock
shared_ptr
expired
expired
if(!wptr.expired()) something_assuming_the_resource_is_still_alive();

TA貢獻1829條經驗 獲得超7個贊
Person
House
std::unique_ptr<T>
.
std::shared_ptr<T>
.std::shared_ptr<T>
std::weak_ptr<T>
T*
T&
.
如果您有一個自定義刪除器(例如,您使用分配池),那么這將導致每個指針的開銷,這很容易通過手動刪除來避免。 std::shared_ptr
具有復制上的引用計數增量的開銷,加上銷毀時的減少,然后進行0計數檢查并刪除所保存的對象。根據實現(xiàn)的不同,這會使您的代碼膨脹并導致性能問題。 編譯時間。與所有模板一樣,智能指針對編譯時間有負面貢獻。
struct BinaryTree{ Tree* m_parent; std::unique_ptr<BinaryTree> m_children[2]; // or use std::array...};
nullptr
std::unique_ptr
.
struct ListNode{ std::shared_ptr<ListNode> m_next; std::weak_ptr<ListNode> m_prev;};
shared_ptr
weak_ptr
- 3 回答
- 0 關注
- 516 瀏覽
添加回答
舉報