c++11 智能指針 unique_ptr、shared_ptr與weak_ptr
C++11中有unique_ptr、shared_ptr與weak_ptr等智能指針(smart pointer),定義在<memory>中。html
能夠對動態資源進行管理,保證任何狀況下,已構造的對象最終會銷燬,即它的析構函數最終會被調用。ios
unique_ptrc++
unique_ptr持有對對象的獨有權,同一時刻只能有一個unique_ptr指向給定對象(經過禁止拷貝語義、只有移動語義來實現)。函數
unique_ptr指針自己的生命週期:從unique_ptr指針建立時開始,直到離開做用域。post
離開做用域時,若其指向對象,則將其所指對象銷燬(默認使用delete操做符,用戶可指定其餘操做)。url
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <vector> #include <map> void mytest() { std::unique_ptr<int> up1(new int(11)); // 沒法複製的unique_ptr //unique_ptr<int> up2 = up1; // err, 不能經過編譯 std::cout << *up1 << std::endl; // 11 std::unique_ptr<int> up3 = std::move(up1); // 如今p3是數據的惟一的unique_ptr std::cout << *up3 << std::endl; // 11 //std::cout << *up1 << std::endl; // err, 運行時錯誤 up3.reset(); // 顯式釋放內存 up1.reset(); // 不會致使運行時錯誤 //std::cout << *up3 << std::endl; // err, 運行時錯誤 std::unique_ptr<int> up4(new int(22)); // 沒法複製的unique_ptr up4.reset(new int(44)); //"綁定"動態對象 std::cout << *up4 << std::endl; // 44 up4 = nullptr;//顯式銷燬所指對象,同時智能指針變爲空指針。與up4.reset()等價 std::unique_ptr<int> up5(new int(55)); int *p = up5.release(); //只是釋放控制權,不會釋放內存 std::cout << *p << std::endl; //cout << *up5 << endl; // err, 運行時錯誤 delete p; //釋放堆區資源 return; } int main() { mytest(); system("pause"); return 0; }
shared_ptrspa
shared_ptr容許多個該智能指針共享第「擁有」同一堆分配對象的內存,這經過引用計數(reference counting)實現,會記錄有多少個shared_ptr共同指向一個對象,一旦最後一個這樣的指針被銷燬,也就是一旦某個對象的引用計數變爲0,這個對象會被自動刪除。指針
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <vector> #include <map> void mytest() { std::shared_ptr<int> sp1(new int(22)); std::shared_ptr<int> sp2 = sp1; std::cout << "cout: " << sp2.use_count() << std::endl; // 打印引用計數 std::cout << *sp1 << std::endl; std::cout << *sp2 << std::endl; sp1.reset(); // 顯示讓引用計數減一 std::cout << "count: " << sp2.use_count() << std::endl; // 打印引用計數 std::cout << *sp2 << std::endl; // 22 return; } int main() { mytest(); system("pause"); return 0; }
weak_ptrc++11
weak_ptr是爲配合shared_ptr而引入的一種智能指針來協助shared_ptr工做,它能夠從一個shared_ptr或另外一個weak_ptr對象構造,它的構造和析構不會引發引用計數的增長或減小。沒有重載 * 和 -> 但可使用lock得到一個可用的shared_ptr對象code
weak_ptr的使用更爲複雜一點,它能夠指向shared_ptr指針指向的對象內存,卻並不擁有該內存,而使用weak_ptr成員lock,則可返回其指向內存的一個share_ptr對象,且在所指對象內存已經無效時,返回指針空值nullptr。
注意:weak_ptr並不擁有資源的全部權,因此不能直接使用資源。
能夠從一個weak_ptr構造一個shared_ptr以取得共享資源的全部權。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <vector> #include <map> void check(std::weak_ptr<int> &wp) { std::shared_ptr<int> sp = wp.lock(); // 轉換爲shared_ptr<int> if (sp != nullptr) { std::cout << "still: " << *sp << std::endl; } else { std::cout << "still: " << "pointer is invalid" << std::endl; } } void mytest() { std::shared_ptr<int> sp1(new int(22)); std::shared_ptr<int> sp2 = sp1; std::weak_ptr<int> wp = sp1; // 指向shared_ptr<int>所指對象 std::cout << "count: " << wp.use_count() << std::endl; // count: 2 std::cout << *sp1 << std::endl; // 22 std::cout << *sp2 << std::endl; // 22 check(wp); // still: 22 sp1.reset(); std::cout << "count: " << wp.use_count() << std::endl; // count: 1 std::cout << *sp2 << std::endl; // 22 check(wp); // still: 22 sp2.reset(); std::cout << "count: " << wp.use_count() << std::endl; // count: 0 check(wp); // still: pointer is invalid return; } int main() { mytest(); system("pause"); return 0; }