<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ### 智能指針 智能指針是一個對象,他用來保存和管理指向堆對象的指針,他會在何時的時刻自動刪除堆對象 常用的有: 1. Qt 中的 QPointer 2. 標準庫中的 `std::shared_ptr/std::unique_ptr/std::weak_ptr` 3. Boost中的 shared_ptr ## std::shared_ptr / std::make_shared 語法 ``` get() 方法來獲取原始指針 reset() 來減少一個引用計數 use_count() 查看一個對象的引用計數 ``` 使用`std::shared_ptr`仍然需要使用`new`來調用,這使得代碼出現了某種程度上的不對稱 ``` void foo(const std::shared_ptr<int>& unique) { (*unique)++; } int main() { std::shared_ptr<int> shared_ptr = std::make_shared<int>(10); foo(shared_ptr); std::cout << *shared_ptr<<"\n"; // 11 shared_ptr = std::make_shared<int>(20); foo(shared_ptr); std::cout << *shared_ptr; // 21 // share_ptr 被復用 } ``` ## std::unique_ptr `std::unique_ptr`是一種獨占的智能指針,它禁止其他智能指針與其共享同一個對象,從而保證代碼的安全 ``` std::unique_ptr<int> pointer = std::make_unique<int>(10); // make_unique 從 C++14 引入 std::unique_ptr<int> pointer2 = pointer; // 非法 ``` `make_unique`并不復雜,C++11 沒有提供`std::make_unique`,可以自行實現: ``` template<typename T, typename ...Args> std::unique_ptr<T> make_unique( Args&& ...args ) { return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) ); } ``` 既然是獨占,換句話說就是不可復制。但是,我們可以利用`std::move`將其轉移給其他的`unique_ptr` ``` #include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo::Foo" << std::endl; } ~Foo() { std::cout << "Foo::~Foo" << std::endl; } void foo() { std::cout << "Foo::foo" << std::endl; } }; void f(const Foo &) { std::cout << "f(const Foo&)" << std::endl; } int main() { std::unique_ptr<Foo> p1(std::make_unique<Foo>()); // p1 不空, 輸出 if (p1) p1->foo(); { std::unique_ptr<Foo> p2(std::move(p1)); // p2 不空, 輸出 f(*p2); // p2 不空, 輸出 if(p2) p2->foo(); // p1 為空, 無輸出 if(p1) p1->foo(); p1 = std::move(p2); // p2 為空, 無輸出 if(p2) p2->foo(); std::cout << "p2 被銷毀" << std::endl; } // p1 不空, 輸出 if (p1) p1->foo(); // Foo 的實例會在離開作用域時被銷毀 } ``` ### std::weak_ptr std::share_ptr 在相互應用的時候,也是無法釋放內存的, 此時就要用到 std::weak_ptr bad ``` #include <iostream> #include <memory> class A; class B{ public: ~B() { std::cout << "B destory, a_ptr use_count:" << a_ptr.use_count() << "\n"; } std::shared_ptr<A> a_ptr; }; class A{ public: ~A() { std::cout << "A destory, b_ptr use_count:" << b_ptr.use_count() << "\n"; } std::shared_ptr<B> b_ptr; }; int main() { std::shared_ptr<A> a(new A()); std::shared_ptr<B> b(new B()); a->b_ptr=b; b->a_ptr=a; std::cout << "A:" << a.use_count() << "\n"; std::cout << "B" << b.use_count() << "\n"; } ``` good ``` class A{ public: ~A() { std::cout << "A destory, b_ptr use_count:" << b_ptr.use_count() << "\n"; } std::weak_ptr<B> b_ptr; }; ```
                  <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>

                              哎呀哎呀视频在线观看