auto_ptr VS unique_ptr

1. unique_ptr is not a direct replacement. The major flaw that it fixes is the implicit transfer of ownership.

std::auto_ptr<int> a(new int(10)), b;
b = a; //implicitly transfers ownership

std::unique_ptr<int> a(new int(10)), b;
b = std::move(a); //ownership must be transferred explicitly

2. On the other hand, unique_ptr will have completely new capabilities: they can be stored in containers.
3. unique_ptr can handle arrays correctly (it will call delete[], whileauto_ptr will attempt to call delete.
發佈了7 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章