auto_ptr使用介绍
生活随笔
收集整理的這篇文章主要介紹了
auto_ptr使用介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1 auto_ptr使用介紹
- 2 auto_ptr的弊端
1 auto_ptr使用介紹
auto_ptr 是c++ 98定義的智能指針模板,其定義了管理指針的對象,可以將new 獲得(直接或間接)的地址賦給這種對象。當對象過期時,其析構函數將使用delete 來釋放內存!
使用建議:
示例代碼如下:
#include <iostream> #include <string> #include <exception> #include <memory>using namespace std;//auto_ptr< Test> t(new Test()); //忠告1: 智能指針不要定義為全局變量class Test { public:Test() {cout << "Test is construct" << endl;debug = 1; }~Test() { cout << "Test is destruct" << endl; }int getDebug() {return debug;}private:int debug; };//用 法: auto_ptr<類型> 變量名(new 類型)void memory_leak_demo1() {auto_ptr< Test> t(new Test());//忠告3: 除非自己知道后果,不要把auto_ptr 智能指針賦值給同類型的另外一個智能指針//auto_ptr< Test> t1;//t1 = t;//auto_ptr<Test>* tp = new auto_ptr<Test>(new Test()); //忠告2: 不要定義指向智能指針對象的指針變量//在使用智能指針訪問對象時,使用方式和普通指針一樣cout<< "-> debug: "<<t->getDebug()<< endl;cout << "* debug: " << (*t).getDebug() << endl;//Test* tmp = t.get();//cout << "get debug: " << tmp->getDebug() << endl;//release 取消指針指針對動態內存的托管,之前分配的內存必須手動釋放//Test* tmp = t.release(); //delete tmp; //reset 重置智能指針托管的內存地址,如果地址不一致,原來的會被析構掉//t.reset();t.reset(new Test());if(0){Test* t1 = new Test();t1->getDebug();}return;}int memory_leak_demo2() {//Test* t = new Test();auto_ptr< Test> t(new Test());/************************************************ 程序執行一段復雜的邏輯,假設嘗試從一個必須存在* 的文件中讀取某些數據,而文件此時不存在************************************************/{throw exception("文件不存在");}//delete t;return 0; }int main() {memory_leak_demo1();/*try {memory_leak_demo2();}catch (exception e) {cout << "catch exception: " << e.what() << endl;}*/system("pause");return 0; }auto_ptr的源碼如下,雖然不能完全看懂還是貼在這里:
#if _HAS_AUTO_PTR_ETC // CLASS TEMPLATE auto_ptr template <class _Ty> class auto_ptr;template <class _Ty> struct auto_ptr_ref { // proxy reference for auto_ptr copyingexplicit auto_ptr_ref(_Ty* _Right) : _Ref(_Right) {}_Ty* _Ref; // generic pointer to auto_ptr ptr };template <class _Ty> class auto_ptr { // wrap an object pointer to ensure destruction public:using element_type = _Ty;explicit auto_ptr(_Ty* _Ptr = nullptr) noexcept : _Myptr(_Ptr) {}auto_ptr(auto_ptr& _Right) noexcept : _Myptr(_Right.release()) {}auto_ptr(auto_ptr_ref<_Ty> _Right) noexcept {_Ty* _Ptr = _Right._Ref;_Right._Ref = nullptr; // release old_Myptr = _Ptr; // reset this}template <class _Other>operator auto_ptr<_Other>() noexcept { // convert to compatible auto_ptrreturn auto_ptr<_Other>(*this);}template <class _Other>operator auto_ptr_ref<_Other>() noexcept { // convert to compatible auto_ptr_ref_Other* _Cvtptr = _Myptr; // test implicit conversionauto_ptr_ref<_Other> _Ans(_Cvtptr);_Myptr = nullptr; // pass ownership to auto_ptr_refreturn _Ans;}template <class _Other>auto_ptr& operator=(auto_ptr<_Other>& _Right) noexcept {reset(_Right.release());return *this;}template <class _Other>auto_ptr(auto_ptr<_Other>& _Right) noexcept : _Myptr(_Right.release()) {}auto_ptr& operator=(auto_ptr& _Right) noexcept {reset(_Right.release());return *this;}auto_ptr& operator=(auto_ptr_ref<_Ty> _Right) noexcept {_Ty* _Ptr = _Right._Ref;_Right._Ref = 0; // release oldreset(_Ptr); // set newreturn *this;}~auto_ptr() noexcept {delete _Myptr;}_NODISCARD _Ty& operator*() const noexcept { #if _ITERATOR_DEBUG_LEVEL == 2_STL_VERIFY(_Myptr, "auto_ptr not dereferencable"); #endif // _ITERATOR_DEBUG_LEVEL == 2return *get();}_NODISCARD _Ty* operator->() const noexcept { #if _ITERATOR_DEBUG_LEVEL == 2_STL_VERIFY(_Myptr, "auto_ptr not dereferencable"); #endif // _ITERATOR_DEBUG_LEVEL == 2return get();}_NODISCARD _Ty* get() const noexcept {return _Myptr;}_Ty* release() noexcept {_Ty* _Tmp = _Myptr;_Myptr = nullptr;return _Tmp;}void reset(_Ty* _Ptr = nullptr) noexcept { // destroy designated object and store new pointerif (_Ptr != _Myptr) {delete _Myptr;}_Myptr = _Ptr;}private:_Ty* _Myptr; // the wrapped object pointer };2 auto_ptr的弊端
auto_ptr是用于C++11之前的智能指針。由于 auto_ptr 基于排他所有權模式:兩個指針不能指向同一個資源,復制或賦值都會改變資源的所有權。auto_ptr 主要有如下幾個問題:
- 復制和賦值會改變資源的所有權,不符合人的直覺。
- 在 STL 容器中使用auto_ptr存在重大風險,因為容器內的元素必需支持可復制(copy constructable)和可賦值(assignable)。
- 不支持對象數組的操作。
所以,C++11用更嚴謹的unique_ptr 取代了auto_ptr!
參考資料:
總結
以上是生活随笔為你收集整理的auto_ptr使用介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows下Socket库的初始化和
- 下一篇: 波形生成设计