{"id":538,"date":"2020-08-26T00:20:58","date_gmt":"2020-08-25T16:20:58","guid":{"rendered":"https:\/\/www.notown.club\/?p=538"},"modified":"2020-08-26T00:21:39","modified_gmt":"2020-08-25T16:21:39","slug":"c-%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0%e4%bb%a5%e5%8f%8a%e5%be%aa%e7%8e%af%e5%bc%95%e7%94%a8%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.notown.top\/?p=538","title":{"rendered":"C++ \u667a\u80fd\u6307\u9488\u7b80\u5355\u5b9e\u73b0\u4ee5\u53ca\u5faa\u73af\u5f15\u7528\u95ee\u9898"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>\u4e00\u3001share_ptr\u7b80\u5355\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> template&lt;typename T&gt; \n class SharePtr\n {\n     T* ptr;\n     int* ref_count;\n public:\n     SharePtr():ptr(nullptr), ref_count(nullptr) {} \/\/\u9ed8\u8ba4\u6784\u9020\n     SharePtr(T* p):ptr(p), ref_count(new int(1)) {} \/\/\u5e26\u53c2\u6784\u9020\n     SharePtr(SharePtr&amp; other) :ptr(other.ptr), ref_count(&amp;(++(*other.ref_count))) {} \/\/\u62f7\u8d1d\u6784\u9020\n     SharePtr&amp; operator=(SharePtr&amp; other)\/\/\u7b49\u53f7\u8d4b\u503c\n     {\n         if (&amp;other == this)\n             return *this;\n         if (ptr)\n         {\n             if (--(*ref_count) == 0)\n             {\n                 delete ref_count;\n                 delete ptr;\n             }\n         }\n         ptr = other.ptr;\n         if (other.ref_count)\n             ref_count = &amp;(++(other.ref_count));\n         else\n             ref_count = nullptr;\n         return *this;\n     }\n     T&amp; operator*()\n     {\n         if (ptr)\n             return *ptr;\n         else\/\/\u5982\u679c\u662f\u7a7a\u6307\u9488\u5c31\u62a5\u9519\n         {\n             throw \"Nullptr Error\";\n             exit(-1);\n         }\n     }\n     T* operator-&gt;()\n     {\n         if (ptr)\n             return ptr;\n         else\/\/\u5982\u679c\u662f\u7a7a\u6307\u9488\u5c31\u62a5\u9519\n         {\n             throw \"Nullptr Error\";\n             exit(-1);\n         }\n     }\n     ~SharePtr()\n     {\n         if (ptr &amp;&amp; --(*ref_count) == 0)\/\/\u5982\u679c\u8ba1\u6570\u51cf\u4e3a0\u5c31\u6790\u6784\n         {\n             delete ptr;\n             delete ref_count;\n         }\n     }\n };<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e8c\u3001unique_ptr\u7b80\u5355\u5b9e\u73b0 <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">template&lt;typename T&gt;\n class UniquePtr\n {\n     T* ptr;\n public:\n     UniquePtr():ptr(nullptr) {}\n     UniquePtr(T* p):ptr(p) {}\n     UniquePtr(const UniquePtr&amp; other) = delete;\/\/\u907f\u514d\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u62f7\u8d1d\u6784\u9020\n     UniquePtr operator=(const UniquePtr&amp; other) = delete;\/\/\u907f\u514d\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u7b49\u53f7\u8d4b\u503c\n     UniquePtr(UniquePtr&amp;&amp; other) noexcept\/\/\u53ea\u5141\u8bb8\u4f7f\u7528\u79fb\u52a8\u6784\u9020\n     {\n         ptr = other.ptr;\n         other.ptr = nullptr;\n     }\n     UniquePtr&amp; operator=(UniquePtr&amp;&amp; other) noexcept\/\/\u53ea\u5141\u8bb8\u4f7f\u7528\u7b49\u53f7\u5b9e\u73b0\u79fb\u52a8\n     {\n         if (ptr)\n             delete ptr;\n         ptr = other.ptr;\n         other.ptr = nullptr;\n         return *this;\n     }\n     T&amp; operator*()\n     {\n         if (ptr)\n             return *ptr;\n         else\n         {\n             throw \"Nullptr Error\";\n             exit(-1);\n         }\n     }\n     T* operator-&gt;()\n     {\n         if (ptr)\n             return ptr;\n         else\n         {\n             throw \"Nullptr Error\";\n             exit(-1);\n         }\n     }\n     ~UniquePtr()\n     {\n         if (ptr)\n             delete ptr;\n     }\n };<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e09\u3001\u5faa\u73af\u5f15\u7528\u95ee\u9898<\/strong>  <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  class B_class; \/\/ \u524d\u7f6e\u58f0\u660e\n  class A_class {\n  public:\n      shared_ptr&lt;B_class&gt; ptr;\n  };\n class B_class {\n  public:\n      shared_ptr&lt;A_class&gt; ptr;\n  };\nint main()\n  {\n      {\/\/\u5728\u4e00\u4e2a\u4f5c\u7528\u57df\u5185\n          shared_ptr&lt;A_class&gt; A(new A_class());\/\/\u667a\u80fd\u6307\u9488A\u6307\u5411\u4e86A\u7c7b\u5bf9\u8c61\n          shared_ptr&lt;B_class&gt; B(new B_class());\/\/\u667a\u80fd\u6307\u9488B\u6307\u5411\u4e86B\u7c7b\u5bf9\u8c61\n          A-&gt;ptr = B;\/\/\u667a\u80fd\u6307\u9488A\u6307\u5411\u7684\u5bf9\u8c61\u4e2d\u7684\u4e00\u4e2aptr\u6210\u5458\u4e5f\u662f\u667a\u80fd\u6307\u9488\uff0c\u5e76\u4e14\u6307\u5411B\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff08\u5373B\u7c7b\u5bf9\u8c61\uff09\n          B-&gt;ptr = A;\/\/\u667a\u80fd\u6307\u9488B\u6307\u5411\u7684\u5bf9\u8c61\u4e2d\u7684\u4e00\u4e2aptr\u6210\u5458\u4e5f\u662f\u667a\u80fd\u6307\u9488\uff0c\u5e76\u4e14\u6307\u5411A\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff08\u5373A\u7c7b\u5bf9\u8c61\uff09\n\/\/\u6240\u4ee5\u73b0\u5728A\u7c7b\u5bf9\u8c61\u548cB\u7c7b\u5bf9\u8c61\u90fd\u5206\u522b\u6709\u4e24\u4e2ashare_ptr\n      }\n\/*\n\u5f53\u79bb\u5f00\u4f5c\u7528\u57df\u662f\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\uff1a\n\u9996\u5148\u662f\u667a\u80fd\u6307\u9488A\u6267\u884c\u6790\u6784\u51fd\u6570\uff0c\u56e0\u4e3a\u6307\u9488A\u6307\u5411\u7684\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u51cf\u53bb1\u540e\uff0c\u8fd8\u52691\uff0c\u6240\u4ee5\u6ca1\u6709\u6267\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u800c\u76f4\u63a5\u8fd4\u56de\uff0c\u4e0d\u91ca\u653e\u5185\u5b58\u3002\n\u7136\u540e\u667a\u80fd\u6307\u9488B\u6267\u884c\u6790\u6784\u51fd\u6570\u4e5f\u540c\u6837\u5982\u6b64\u3002\n\u6240\u4ee5\u5230\u6700\u540eA\u7c7b\u5bf9\u8c61\u548cB\u7c7b\u5bf9\u8c61\u7684\u5185\u5b58\u8fd8\u662f\u6ca1\u6709\u91ca\u653e\u3002\n*\/\n      return 0;\n  }<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u56db\u3001\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528\u5f31\u5f15\u7528weak_ptr\u53ef\u89e3\u51b3\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\uff0c\u56e0\u4e3aweak_ptr\u5e76\u4e0d\u4f1a\u4f7f\u8ba1\u6570\u5668\u52a01\u3002\u6309\u4e0a\u8fb9\u7684\u4f8b\u5b50\u5c31\u662f\u8bf4\u628aA\u7c7b\u548cB\u7c7b\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u6362\u6210weak_ptr\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001share_ptr\u7b80\u5355\u5b9e\u73b0 template&lt;typename T&gt; class ShareP&hellip;<a href=\"https:\/\/www.notown.top\/?p=538\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C++ \u667a\u80fd\u6307\u9488\u7b80\u5355\u5b9e\u73b0\u4ee5\u53ca\u5faa\u73af\u5f15\u7528\u95ee\u9898<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[11],"tags":[],"class_list":["post-538","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=538"}],"version-history":[{"count":2,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/538\/revisions"}],"predecessor-version":[{"id":540,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/538\/revisions\/540"}],"wp:attachment":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}