{"id":395,"date":"2026-04-09T23:53:32","date_gmt":"2026-04-09T15:53:32","guid":{"rendered":"http:\/\/chenglixue.top\/?p=395"},"modified":"2026-04-09T23:59:46","modified_gmt":"2026-04-09T15:59:46","slug":"395","status":"publish","type":"post","link":"http:\/\/chenglixue.top\/?p=395","title":{"rendered":"c++ \u667a\u80fd\u6307\u9488\u603b\u7ed3"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h1>unique_ptr<\/h1>\n<p>\u5728\u4efb\u4f55\u65f6\u523b\uff0c\u53ea\u6709\u4e00\u4e2a<code>unique_ptr<\/code>\u53ef\u4ee5\u62e5\u6709\u6240\u7ba1\u7406\u7684\u5bf9\u8c61\uff0c\u4e0d\u80fd\u591a\u4e2a<\/p>\n<h2>\u673a\u5236<\/h2>\n<ul>\n<li>\u7981\u6b62\u62f7\u8d1d\u6784\u9020\u548c\u62f7\u8d1d\u8d4b\u503c<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u79fb\u52a8\u8bed\u4e49<\/p>\n<\/li>\n<li>\n<p>\u8f7b\u91cf\u7ea7<\/p>\n<p><code>unique_ptr<\/code>\u5728\u5185\u5b58\u4e0a\u975e\u5e38\u8f7b\u91cf\uff0c\u5176\u5927\u5c0f\u901a\u5e38\u4e0e\u4e00\u4e2a\u88f8\u6307\u9488\u76f8\u540c\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u7ef4\u62a4\u5f15\u7528\u8ba1\u6570\u7b49\u989d\u5916\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u81ea\u5b9a\u4e49\u5220\u9664\u5668<\/p>\n<p>\u8fd9\u4f7f\u5f97\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7ba1\u7406\u5806\u5185\u5b58\uff0c\u8fd8\u53ef\u4ee5\u7ba1\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u8d44\u6e90\uff08\u5982\u6587\u4ef6\u53e5\u67c4\u3001\u4e92\u65a5\u9501\u7b49\uff09\uff0c\u5e76\u5728\u8d44\u6e90\u91ca\u653e\u65f6\u6267\u884c\u7279\u5b9a\u7684\u6e05\u7406\u64cd\u4f5c<\/p>\n<pre><code class=\"line-numbers\">void file_deleter(FILE* fp) {\n  if (fp) {\n      fclose(fp);\n      std::cout &lt;&lt; \"File closed by custom deleter.\" &lt;&lt; std::endl;\n  }\n}\n\nvoid test_unique_ptr_custom_deleter() {\n  \/\/ unique_ptr&lt;FILE, decltype(&amp;file_deleter)&gt;\n  std::unique_ptr&lt;FILE, void(*)(FILE*)&gt; file_ptr(fopen(\"test.txt\", \"w\"), file_deleter);\n  if (file_ptr) {\n      fprintf(file_ptr.get(), \"Hello from unique_ptr!\");\n  }\n  \/\/ file_ptr \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cfile_deleter \u4f1a\u88ab\u8c03\u7528\n}\n<\/code><\/pre>\n<\/li>\n<li>\u652f\u6301\u6570\u7ec4<\/p>\n<\/li>\n<li>\n<p>\u5355\u6b21\u5185\u5b58\u5206\u914d<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u5b89\u5168<\/p>\n<p><code>make_unique<\/code>\u5c06\u5bf9\u8c61\u7684\u521b\u5efa\u5c01\u88c5\u5728\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u4e2d\uff0c\u907f\u514d\u4e86\u5728\u8868\u8fbe\u5f0f\u6c42\u503c\u8fc7\u7a0b\u4e2d\u56e0\u5f02\u5e38\u800c\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f<\/p>\n<\/li>\n<\/ul>\n<h1>shared_ptr<\/h1>\n<p>\u591a\u4e2a<code>shared_ptr<\/code>\u5b9e\u4f8b\u53ef\u4ee5\u5171\u540c\u62e5\u6709\u5e76\u7ba1\u7406\u540c\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\u3002\u5f53\u6307\u5411\u8be5\u5bf9\u8c61\u7684\u6700\u540e\u4e00\u4e2a<code>shared_ptr<\/code>\u88ab\u9500\u6bc1\u6216\u91cd\u7f6e\u65f6\uff0c\u8be5\u5bf9\u8c61\u624d\u4f1a\u88ab\u81ea\u52a8\u6790\u6784\u5e76\u91ca\u653e\u5176\u5360\u7528\u7684\u5185\u5b58<\/p>\n<h2>\u673a\u5236<\/h2>\n<ul>\n<li>\u6bcf\u4e2a<code>shared_ptr<\/code>\u5bf9\u8c61\u90fd\u4e0e\u4e00\u4e2a\u5185\u90e8\u7684\u8ba1\u6570\u5668\u5173\u8054\uff0c\u8fd9\u4e2a\u8ba1\u6570\u5668\u8bb0\u5f55\u4e86\u5f53\u524d\u6709\u591a\u5c11\u4e2a<code>shared_ptr<\/code>\u5b9e\u4f8b\u6b63\u5728\u7ba1\u7406\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u8fd9\u4e2a\u8ba1\u6570\u5668\u901a\u5e38\u88ab\u79f0\u4e3a\u201c<strong>\u5f3a\u5f15\u7528\u8ba1\u6570<\/strong>\u201d<\/p>\n<\/li>\n<li>\n<p>\u6784\u9020\uff08\u589e\u52a0\uff09<\/p>\n<p>\u5f53\u4e00\u4e2a\u65b0\u7684<code>shared_ptr<\/code>\u88ab\u521b\u5efa\u5e76\u6307\u5411\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u5f3a\u5f15\u7528\u8ba1\u6570\u4f1a\u589e\u52a01<\/p>\n<\/li>\n<li>\n<p>\u8d4b\u503c\uff08\u589e\u51cf\uff09<\/p>\n<p>\u5f53\u4e00\u4e2a<code>shared_ptr<\/code>\u88ab\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a<code>shared_ptr<\/code>\u65f6\uff0c\u88ab\u8d4b\u503c\u7684<code>shared_ptr<\/code>\u4f1a\u653e\u5f03\u5b83\u539f\u6765\u7ba1\u7406\u7684\u5bf9\u8c61\uff0c\u5bfc\u81f4\u5176\u539f\u5bf9\u8c61\u7684\u5f3a\u5f15\u7528\u8ba1\u6570\u51cf\u5c111\u3002\u7136\u540e\uff0c\u5b83\u4f1a\u5f00\u59cb\u7ba1\u7406\u65b0\u5bf9\u8c61\uff0c\u5bfc\u81f4\u65b0\u5bf9\u8c61\u7684\u5f3a\u5f15\u7528\u8ba1\u6570\u589e\u52a01<\/p>\n<\/li>\n<li>\n<p>\u6790\u6784\/\u91cd\u7f6e\uff08\u51cf\u5c11\uff09<\/p>\n<p>\u5f53\u4e00\u4e2a<code>shared_ptr<\/code>\u5b9e\u4f8b\u88ab\u9500\u6bc1\u6216\u88ab\u663e\u5f0f\u5730\u91cd\u7f6e\u65f6\uff0c\u5b83\u6240\u7ba1\u7406\u7684\u5bf9\u8c61\u7684\u5f3a\u5f15\u7528\u8ba1\u6570\u4f1a\u51cf\u5c111<\/p>\n<\/li>\n<li>\n<p>\u8d44\u6e90\u91ca\u653e<\/p>\n<p>\u5f53\u5f3a\u5f15\u7528\u8ba1\u6570\u51cf\u5c11\u52300\u65f6\uff0c<code>shared_ptr<\/code>\u4f1a\u81ea\u52a8\u8c03\u7528\u88ab\u7ba1\u7406\u5bf9\u8c61\u7684\u6790\u6784\u51fd\u6570\uff0c\u5e76\u91ca\u653e\u5176\u5360\u7528\u7684\u5185\u5b58<\/p>\n<\/li>\n<li>\n<p>\u5355\u6b21\u5185\u5b58\u5206\u914d<\/p>\n<p><code>make_shared<\/code>\u53ea\u8fdb\u884c\u4e00\u6b21\u5185\u5b58\u5206\u914d\uff0c\u540c\u65f6\u4e3a\u5bf9\u8c61\u548c\u63a7\u5236\u5757\u5206\u914d\u5185\u5b58\uff0c\u51cf\u5c11\u4e86\u5185\u5b58\u788e\u7247\uff0c\u63d0\u9ad8\u4e86\u7f13\u5b58\u5c40\u90e8\u6027<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u5b89\u5168<\/p>\n<p><code>make_shared<\/code>\u5c06\u5bf9\u8c61\u7684\u521b\u5efa\u548c<code>shared_ptr<\/code>\u7684\u6784\u9020\u5c01\u88c5\u5728\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u4e2d\uff0c\u907f\u514d\u4e86\u5728\u8868\u8fbe\u5f0f\u6c42\u503c\u8fc7\u7a0b\u4e2d\u56e0\u5f02\u5e38\u800c\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f<\/p>\n<\/li>\n<\/ul>\n<h2>\u63a7\u5236\u5757<\/h2>\n<p><code>shared_ptr<\/code>\u4e4b\u6240\u4ee5\u80fd\u591f\u5b9e\u73b0\u5982\u6b64\u7cbe\u5de7\u7684\u5f15\u7528\u8ba1\u6570\u548c\u8d44\u6e90\u7ba1\u7406\uff0c\u79bb\u4e0d\u5f00\u5176\u80cc\u540e\u7684\u6838\u5fc3\u7ec4\u4ef6\u2014\u2014\u63a7\u5236\u5757<\/p>\n<p>\u4e00\u4e2a<code>std::shared_ptr<\/code>\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u901a\u5e38\u7531\u4e24\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n<ol>\n<li>\u539f\u59cb\u6307\u9488\uff1a\u6307\u5411\u5b83\u6240\u7ba1\u7406\u7684\u5b9e\u9645\u5bf9\u8c61<\/li>\n<li>\u63a7\u5236\u5757\u6307\u9488\uff1a\u6307\u5411\u4e00\u4e2a\u72ec\u7acb\u4e8e\u88ab\u7ba1\u7406\u5bf9\u8c61\u7684\u5185\u5b58\u533a\u57df\uff0c<strong>\u5b58\u50a8\u4e0e\u8be5\u5bf9\u8c61\u751f\u547d\u5468\u671f\u7ba1\u7406\u76f8\u5173\u7684\u4fe1\u606f<\/strong>\u3002\u6240\u6709\u5171\u4eab\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684<code>shared_ptr<\/code>\u5b9e\u4f8b\u90fd\u6307\u5411\u540c\u4e00\u4e2a\u63a7\u5236\u5757<\/li>\n<\/ol>\n<p>\u63a7\u5236\u5757\u901a\u5e38\u5305\u542b\u4ee5\u4e0b\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li>\u5f3a\u5f15\u7528\u8ba1\u6570\uff1a\u8bb0\u5f55\u6709\u591a\u5c11\u4e2a<code>std::shared_ptr<\/code>\u5b9e\u4f8b\u6b63\u5728\u7ba1\u7406\u8be5\u5bf9\u8c61\u3002\u5f53\u6b64\u8ba1\u6570\u5f52\u96f6\u65f6\uff0c\u88ab\u7ba1\u7406\u5bf9\u8c61\u5c06\u88ab\u6790\u6784\u5e76\u91ca\u653e<\/p>\n<\/li>\n<li>\n<p>\u5f31\u5f15\u7528\u8ba1\u6570\uff1a\u8bb0\u5f55\u6709\u591a\u5c11\u4e2a<code>std::weak_ptr<\/code>\u5b9e\u4f8b\u6b63\u5728\u89c2\u5bdf\u8be5\u5bf9\u8c61<\/p>\n<p>\u5373\u4f7f\u5f3a\u5f15\u7528\u8ba1\u6570\u5f52\u96f6\uff0c\u53ea\u8981\u5f31\u5f15\u7528\u8ba1\u6570\u4e0d\u4e3a\u96f6\uff0c\u63a7\u5236\u5757\u5c31\u4e0d\u4f1a\u88ab\u9500\u6bc1<\/p>\n<\/li>\n<li>\n<p>\u539f\u59cb\u6307\u9488<\/p>\n<\/li>\n<li>\n<p>\u81ea\u5b9a\u4e49\u5220\u9664\u5668<\/p>\n<\/li>\n<li>\n<p>\u81ea\u5b9a\u4e49\u5206\u914d\u5668<\/p>\n<\/li>\n<\/ul>\n<p>\u751f\u547d\u5468\u671f\uff1a\u63a7\u5236\u5757\u53ea\u6709\u5728\u5f3a\u5f15\u7528\u8ba1\u6570\u548c\u5f31\u5f15\u7528\u8ba1\u6570\u90fd\u5f52\u96f6\u65f6\u624d\u4f1a\u88ab\u9500\u6bc1<\/p>\n<h2>\u7b80\u5316\u7248shared_ptr<\/h2>\n<pre><code class=\"line-numbers\">template &lt;typename T&gt;\nclass MySharedPtr {\npublic:\n    \/\/ \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\n    MySharedPtr() : m_ptr(nullptr), m_ref_count(newint(0)) {\n        std::cout &lt;&lt; \"Default constructor called. Ref count: \" &lt;&lt; *m_ref_count &lt;&lt; std::endl;\n    }\n\n    \/\/ \u63a5\u53d7\u88f8\u6307\u9488\u7684\u6784\u9020\u51fd\u6570\n    explicit MySharedPtr(T* ptr) : m_ptr(ptr), m_ref_count(new int(1)) {\n        std::cout &lt;&lt; \"Constructor with raw pointer called. Ref count: \" &lt;&lt; *m_ref_count &lt;&lt; std::endl;\n    }\n\n    \/\/ \u62f7\u8d1d\u6784\u9020\u51fd\u6570\n    MySharedPtr(const MySharedPtr&amp; other) : m_ptr(other.m_ptr), m_ref_count(other.m_ref_count) {\n        if (m_ref_count) {\n            (*m_ref_count)++;\n        }\n        std::cout &lt;&lt; \"Copy constructor called. Ref count: \" &lt;&lt; *m_ref_count &lt;&lt; std::endl;\n    }\n\n    \/\/ \u8d4b\u503c\u8fd0\u7b97\u7b26\n    MySharedPtr&amp; operator=(const MySharedPtr&amp; other) {\n        if (this == &amp;other) { \/\/ \u5904\u7406\u81ea\u8d4b\u503c\n            return *this;\n        }\n\n        \/\/ \u51cf\u5c11\u65e7\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\n        if (m_ref_count) {\n            (*m_ref_count)--;\n            if (*m_ref_count == 0) {\n                delete m_ptr;\n                delete m_ref_count;\n                std::cout &lt;&lt; \"Old object and ref count deleted in assignment.\" &lt;&lt; std::endl;\n            }\n        }\n\n        \/\/ \u62f7\u8d1d\u65b0\u5bf9\u8c61\u7684\u4fe1\u606f\u5e76\u589e\u52a0\u5f15\u7528\u8ba1\u6570\n        m_ptr = other.m_ptr;\n        m_ref_count = other.m_ref_count;\n        if (m_ref_count) {\n            (*m_ref_count)++;\n        }\n        std::cout &lt;&lt; \"Assignment operator called. Ref count: \" &lt;&lt; *m_ref_count &lt;&lt; std::endl;\n        return *this;\n    }\n\n    \/\/ \u6790\u6784\u51fd\u6570\n    ~MySharedPtr() {\n        if (m_ref_count) {\n            (*m_ref_count)--;\n            std::cout &lt;&lt; \"Destructor called. Current ref count: \" &lt;&lt; *m_ref_count &lt;&lt; std::endl;\n            if (*m_ref_count == 0) {\n                delete m_ptr; \/\/ \u91ca\u653e\u88ab\u7ba1\u7406\u5bf9\u8c61\n                delete m_ref_count; \/\/ \u91ca\u653e\u5f15\u7528\u8ba1\u6570\u5668\n                m_ptr = nullptr;\n                m_ref_count = nullptr;\n                std::cout &lt;&lt; \"Object and ref count deleted.\" &lt;&lt; std::endl;\n            }\n        }\n    }\n\n    \/\/ \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\n    T&amp; operator*() const {\n        return *m_ptr;\n    }\n\n    \/\/ \u7bad\u5934\u8fd0\u7b97\u7b26\n    T* operator-&gt;() const {\n        return m_ptr;\n    }\n\n    \/\/ \u83b7\u53d6\u5f15\u7528\u8ba1\u6570\n    int use_count() const {\n        return m_ref_count ? *m_ref_count : 0;\n    }\n\n    \/\/ \u83b7\u53d6\u539f\u59cb\u6307\u9488\n    T* get() const {\n        return m_ptr;\n    }\n\nprivate:\n    T* m_ptr;         \/\/ \u6307\u5411\u88ab\u7ba1\u7406\u5bf9\u8c61\u7684\u6307\u9488\n    int* m_ref_count; \/\/ \u6307\u5411\u5f15\u7528\u8ba1\u6570\u5668\u7684\u6307\u9488\n};\n<\/code><\/pre>\n<h1>weak_ptr<\/h1>\n<p><code>weak_ptr<\/code>\u6307\u5411\u4e00\u4e2a\u7531<code>shared_ptr<\/code>\u7ba1\u7406\u7684\u5bf9\u8c61\uff0c\u4f46<strong>\u4e0d\u4f1a\u589e\u52a0\u5bf9\u8c61\u7684\u5f3a\u5f15\u7528\u8ba1\u6570<\/strong><\/p>\n<h2>\u673a\u5236<\/h2>\n<ul>\n<li>\u4e0d\u589e\u52a0\u5f3a\u5f15\u7528\u8ba1\u6570\n<p><strong>\u53ea\u662f\u201c\u89c2\u5bdf\u201d\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u201c\u62e5\u6709\u201d\u5bf9\u8c61<\/strong><\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u5b58\u6d3b<\/p>\n<p>\u901a\u8fc7<code>lock()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<code>lock()<\/code>\u4f1a\u5c1d\u8bd5\u8fd4\u56de\u4e00\u4e2a<code>shared_ptr<\/code>\u3002\u5982\u679c\u5bf9\u8c61\u4ecd\u7136\u5b58\u6d3b\uff0c<code>lock()<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6709\u6548\u7684<code>shared_ptr<\/code>\uff0c\u5e76\u589e\u52a0\u5bf9\u8c61\u7684\u5f3a\u5f15\u7528\u8ba1\u6570\uff1b\u5982\u679c\u5bf9\u8c61\u5df2\u7ecf\u9500\u6bc1\uff0c<code>lock()<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684<code>shared_ptr<\/code><\/p>\n<\/li>\n<li>\n<p>\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5bf9\u8c61<\/p>\n<p>\u5fc5\u987b\u901a\u8fc7<code>lock()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a<code>shared_ptr<\/code>\u624d\u80fd\u8bbf\u95ee<\/p>\n<\/li>\n<\/ul>\n<h2>\u5faa\u73af\u5f15\u7528<\/h2>\n<pre><code class=\"line-numbers\">class B;\n\nclass A {\npublic:\n    std::shared_ptr&lt;B&gt; b_ptr;\n    A() { std::cout &lt;&lt; \"A constructor\" &lt;&lt; std::endl; }\n    ~A() { std::cout &lt;&lt; \"A destructor\" &lt;&lt; std::endl; }\n};\n\nclass B {\npublic:\n    std::shared_ptr&lt;A&gt; a_ptr;\n    B() { std::cout &lt;&lt; \"B constructor\" &lt;&lt; std::endl; }\n    ~B() { std::cout &lt;&lt; \"B destructor\" &lt;&lt; std::endl; }\n};\n\nvoid test_circular_reference() {\n    std::shared_ptr&lt;A&gt; pa = std::make_shared&lt;A&gt;();\n    std::shared_ptr&lt;B&gt; pb = std::make_shared&lt;B&gt;();\n\n    pa-&gt;b_ptr = pb; \/\/ A \u62e5\u6709 B\n    pb-&gt;a_ptr = pa; \/\/ B \u62e5\u6709 A\n\n    std::cout &lt;&lt; \"pa use_count: \" &lt;&lt; pa.use_count() &lt;&lt; std::endl; \/\/ \u8f93\u51fa 2\n    std::cout &lt;&lt; \"pb use_count: \" &lt;&lt; pb.use_count() &lt;&lt; std::endl; \/\/ \u8f93\u51fa 2\n\n    \/\/ \u5f53 pa \u548c pb \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u4eec\u7684\u5f3a\u5f15\u7528\u8ba1\u6570\u4f1a\u51cf\u5230 1\uff0c\u4f46\u6c38\u8fdc\u4e0d\u4f1a\u5f52\u96f6\n    \/\/ A \u548c B \u7684\u6790\u6784\u51fd\u6570\u90fd\u4e0d\u4f1a\u88ab\u8c03\u7528\uff0c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\n}\n<\/code><\/pre>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528<code>weak_ptr<\/code>\u6253\u7834\u5faa\u73af\u3002\u5c06\u5faa\u73af\u5f15\u7528\u4e2d\u7684\u4e00\u65b9\u6539\u4e3a<code>weak_ptr<\/code>\uff0c\u53ef\u4ee5\u6253\u7834\u8fd9\u79cd\u5faa\u73af<\/p>\n<pre><code class=\"line-numbers\">class B_fixed;\n\nclass A_fixed {\npublic:\n    std::shared_ptr&lt;B_fixed&gt; b_ptr;\n    A_fixed() { std::cout &lt;&lt; \"A_fixed constructor\" &lt;&lt; std::endl; }\n    ~A_fixed() { std::cout &lt;&lt; \"A_fixed destructor\" &lt;&lt; std::endl; }\n};\n\nclass B_fixed {\npublic:\n    std::weak_ptr&lt;A_fixed&gt; a_ptr; \/\/ \u5c06\u5f3a\u5f15\u7528\u6539\u4e3a\u5f31\u5f15\u7528\n    B_fixed() { std::cout &lt;&lt; \"B_fixed constructor\" &lt;&lt; std::endl; }\n    ~B_fixed() { std::cout &lt;&lt; \"B_fixed destructor\" &lt;&lt; std::endl; }\n};\n\nvoid test_circular_reference_fixed() {\n    std::shared_ptr&lt;A_fixed&gt; pa = std::make_shared&lt;A_fixed&gt;();\n    std::shared_ptr&lt;B_fixed&gt; pb = std::make_shared&lt;B_fixed&gt;();\n\n    pa-&gt;b_ptr = pb; \/\/ A_fixed \u62e5\u6709 B_fixed\n    pb-&gt;a_ptr = pa; \/\/ B_fixed \u89c2\u5bdf A_fixed\uff0c\u4e0d\u589e\u52a0\u5f3a\u5f15\u7528\u8ba1\u6570\n\n    std::cout &lt;&lt; \"pa use_count: \" &lt;&lt; pa.use_count() &lt;&lt; std::endl; \/\/ \u8f93\u51fa 1\n    std::cout &lt;&lt; \"pb use_count: \" &lt;&lt; pb.use_count() &lt;&lt; std::endl; \/\/ \u8f93\u51fa 1\n\n    \/\/ \u5f53 pa \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cA_fixed \u5bf9\u8c61\u7684\u5f3a\u5f15\u7528\u8ba1\u6570\u53d8\u4e3a 0\uff0cA_fixed \u88ab\u9500\u6bc1\n    \/\/ \u6b64\u65f6 pb-&gt;a_ptr \u4f1a\u5931\u6548\n    \/\/ \u5f53 pb \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cB_fixed \u5bf9\u8c61\u7684\u5f3a\u5f15\u7528\u8ba1\u6570\u53d8\u4e3a 0\uff0cB_fixed \u88ab\u9500\u6bc1\n    \/\/ \u5185\u5b58\u5f97\u5230\u6b63\u786e\u91ca\u653e\n}\n<\/code><\/pre>\n<h1>\u603b\u7ed3<\/h1>\n<ul>\n<li>\u4f18\u5148\u4f7f\u7528<code>unique_ptr<\/code><\/p>\n<\/li>\n<li>\n<p>\u5f53\u9700\u8981\u5171\u4eab\u6240\u6709\u6743\u65f6\u4f7f\u7528<code>shared_ptr<\/code><\/p>\n<\/li>\n<li>\n<p>\u8b66\u60d5\u5e76\u89e3\u51b3<code>shared_ptr<\/code>\u7684\u5faa\u73af\u5f15\u7528<\/p>\n<\/li>\n<li>\n<p>\u4f18\u5148\u4f7f\u7528<code>std::make_unique<\/code>\u548c<code>std::make_shared<\/code><\/p>\n<p>\u4e0d\u4ec5\u63d0\u4f9b\u5f02\u5e38\u5b89\u5168\uff0c\u8fd8\u80fd\u51cf\u5c11\u5185\u5b58\u5206\u914d\u6b21\u6570<\/p>\n<\/li>\n<li>\n<p>\u907f\u514d\u88f8\u6307\u9488\u4e0e\u667a\u80fd\u6307\u9488\u6df7\u7528<\/p>\n<\/li>\n<li>\n<p>\u5229\u7528\u81ea\u5b9a\u4e49\u5220\u9664\u5668\u7ba1\u7406\u975e\u5185\u5b58\u8d44\u6e90<\/p>\n<\/li>\n<li>\n<p>shared_ptr\u7ba1\u7406\u7684\u5bf9\u8c61\u672c\u8eab\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684<\/p>\n<p><strong>shared_ptr\u7684\u5f15\u7528\u8ba1\u6570(\u5185\u5b58\u7ba1\u7406)\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u5176\u6240\u7ba1\u7406\u7684\u5bf9\u8c61\u7684\u6570\u636e\u6210\u5458\u7684\u8bbf\u95ee\u5e76\u4e0d\u662f<\/strong><\/p>\n<ul>\n<li>\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u62f7\u8d1d\u3001\u8d4b\u503c\u3001\u9500\u6bc1\u540c\u4e00\u4e2a <code>shared_ptr<\/code> \u5bf9\u8c61\uff0c\u5176\u5185\u90e8\u7684\u5f15\u7528\u8ba1\u6570\u4f1a\u901a\u8fc7\u539f\u5b50\u64cd\u4f5c\u6b63\u786e\u7ef4\u62a4<\/li>\n<li>\u5f53\u6700\u540e\u4e00\u4e2a <code>shared_ptr<\/code> \u5b9e\u4f8b\u88ab\u9500\u6bc1\u65f6\uff0c\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\uff08\u5373\u539f\u59cb\u5bf9\u8c61\uff09\u4f1a\u88ab\u5b89\u5168\u5730\u91ca\u653e\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e5f\u662f\u7ebf\u7a0b\u5b89\u5168\u7684<\/li>\n<li><code>shared_ptr<\/code> \u4ec5\u4ec5\u4fdd\u8bc1\u4e86\u5b83\u5bf9\u5185\u5b58\u7684\u7ba1\u7406\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u5b83\u4e0d\u4fdd\u8bc1\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u6216\u4fee\u6539\u5176\u5185\u90e8\u6307\u5411\u7684\u539f\u59cb\u5bf9\u8c61\u65f6\u4e0d\u4f1a\u53d1\u751f\u6570\u636e\u7ade\u4e89<\/li>\n<li>\u5982\u679c\u539f\u59cb\u5bf9\u8c61\u7684\u6570\u636e\u6210\u5458\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u88ab\u5e76\u53d1\u4fee\u6539\uff0c\u4ecd\u9700\u81ea\u884c\u6dfb\u52a0\u540c\u6b65\u673a\u5236\uff08\u5982 <code>std::mutex<\/code>\uff09\u6765\u4fdd\u62a4\u5bf9\u539f\u59cb\u5bf9\u8c61\u7684\u8bbf\u95ee<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>unique_ptr \u5728\u4efb\u4f55\u65f6\u523b\uff0c\u53ea\u6709\u4e00\u4e2aunique_ptr\u53ef\u4ee5\u62e5\u6709\u6240\u7ba1\u7406\u7684\u5bf9\u8c61\uff0c\u4e0d\u80fd\u591a\u4e2a \u673a\u5236 \u7981\u6b62\u62f7\u8d1d\u6784\u9020\u548c\u62f7\u8d1d\u8d4b\u503c \u652f\u6301\u79fb &#8230;<\/p>","protected":false},"author":1,"featured_media":396,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/395"}],"collection":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=395"}],"version-history":[{"count":3,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/395\/revisions"}],"predecessor-version":[{"id":399,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/395\/revisions\/399"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/media\/396"}],"wp:attachment":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}