<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                >[warning] `[11!]` `[17-]` >[danger] 該對象位于頭文件 `memory` ## [$]一.什么是auto_ptr + `auto_ptr` 是 C++標準庫提供的類模板 它可以幫助程序員自動管理用 new 表達式動態分配的 **單個對象**。 >對用 new 表達式分配的數組管理沒有類似的支持。 + `auto_ptr` 對象被初始化為指向由new表達式創建的動態分配對象。當 `auto_ptr` 對象的生命期結束時,動態分配的對象被自動釋放。 ## [$]二.auto_ptr的基本操作 1. 定義一個 `auto_ptr` 的對象 ```c++ //定義auto_ptr的對象的三種形式 auto_ptr<指向的類型> 標識符(由new表達式返回的對象地址); auto_ptr<指向的類型> 標識符(另一個auto_ptr對象); auto_ptr<指向的類型> 標識符; //案例 auto_ptr<int> int_demo(new int(2020)); auto_ptr<string> string_demo(new string("cxk")); ``` 2. 獲取對象的值:使用運算符 `*`,訪問對象的成員使用運算符 `->` ```c++ //這像極了指針 cout<<*int_demo;//輸出2020 cout<<string_demo->size();//3 ``` >和指針一樣,對沒有指向任何對象的auto_ptr進行這些操作會導致內存的非法訪問。 3. 獲取對象的底層指針: `get()` 操作和 `release()` 操作 ```c++ cout<<int_demo.get();//輸出指針 cout<<int_demo.release();//輸出指針并取消自身刪除權限 ``` 4. 重置(修改底層指針): `reset()` 操作 ```c++ int_demo.reset(new int(2021)); ``` >不能使用賦值語句來使它重置 >[info]當int_demo擁有刪除權限時,int_demo會先刪除原有對象。 5. 對字符串重新賦值: `assign()` 操作 >直接覆蓋原有字符串,比刪除并重新分配新對象更快 ```c++ string_demo.assign('hello'); ``` ## [$]三.auto_ptr的刪除權限 1. 當使用一個auto_ptr對象初始化另一個auto_ptr對象,或對auto_ptr對象使用賦值語句時,為了避免重復刪除,原auto_ptr對象失去了刪除權限,而新auto_ptr對象則擁有刪除權限。 ```c++ //案例1 auto_ptr<string> b(new string("cxk")),c; c=b;//此時b失去刪除權限,而c擁有刪除權限 cout<<b->size(); //案例2 auto_ptr<string> b(new string("cxk")),c(b);//此時b失去刪除權限,而c擁有刪除權限 cout<<b->size(); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看