{"id":2431,"date":"2024-07-22T11:15:47","date_gmt":"2024-07-22T03:15:47","guid":{"rendered":"https:\/\/courtship.top\/?p=2431"},"modified":"2024-07-22T11:15:47","modified_gmt":"2024-07-22T03:15:47","slug":"%e5%ae%9e%e7%8e%b0%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2024\/07\/22\/%e5%ae%9e%e7%8e%b0%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88\/","title":{"rendered":"\u5b9e\u73b0\u667a\u80fd\u6307\u9488"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Unique_ptr<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0\u76ee\u6807<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u57fa\u4e8e\u6392\u4ed6\u6240\u6709\u6743\u6a21\u5f0f:\u4e24\u4e2a\u6307\u9488\u4e0d\u80fd\u6307\u5411\u540c\u4e00\u4e2a\u8d44\u6e90\u3002<\/li>\n\n\n\n<li>\u7531\u4e8e\u72ec\u5360\u5bf9\u8c61\u7684\u62e5\u6709\u6743,\u6240\u4ee5\u4e0d\u63d0\u4f9b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u5de6\u503c\u8d4b\u503c\u51fd\u6570\u91cd\u8f7d\u3002<\/li>\n\n\n\n<li>\u63d0\u4f9b\u79fb\u52a8\u6784\u9020\u548c\u79fb\u52a8\u8d4b\u503c\u51fd\u6570\u3002<\/li>\n\n\n\n<li>\u4e3a\u4e86\u5b9e\u73b0\u5355\u4e2a\u5bf9\u8c61\u548c\u4e00\u7ec4\u5bf9\u8c61\u7684\u7ba1\u7406,\u6dfb\u52a0\u4e86\u5220\u9664\u5668\u7c7b\u578b\u3002<\/li>\n\n\n\n<li>\u5728\u5bb9\u5668\u4fdd\u5b58\u6307\u9488\u662f\u5b89\u5168\u3002<\/li>\n\n\n\n<li>unique_ptr\u5177\u6709->\u548c*\u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b26\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u50cf\u666e\u901a\u6307\u9488\u4e00<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0\u7ec6\u8282<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>template&lt;class _Ty, class _Dx = Default_deleter&lt;_Ty>>\nclass My_unique_ptr\n{\npublic:\n \n\t\/*explicit  \u6307\u5b9a\u6784\u9020\u51fd\u6570\u6216\u8f6c\u6362\u51fd\u6570 (C++11 \u8d77)\u6216\u63a8\u5bfc\u6307\u5f15 (C++17 \u8d77)\u4e3a\u663e\u5f0f\uff0c\u5373\u5b83\u4e0d\u80fd\u7528\u4e8e\u9690\u5f0f\u8f6c\u6362\u548c\u590d\u5236\u521d\u59cb\u5316\u3002\n\t*\/\n\texplicit My_unique_ptr(_Ty* ptr = nullptr) :m_ptr(ptr) { std::cout &lt;&lt; \"creat My_unique_ptr\"; }\n\t~My_unique_ptr() { m_deleter(m_ptr); }\n\t\npublic:\n\tusing pointer = _Ty*;\n\tusing element_type = _Ty;\n\tusing deleter_type = _Dx;\n\tpointer m_ptr;\n\t_Dx m_deleter;\n};<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u663e\u793a\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*explicit*\/My_unique_ptr(_Ty* ptr = nullptr) :m_ptr(ptr) \n{     std::cout &lt;&lt; \"creat My_unique_ptr\";\n}\n~My_unique_ptr() \n{ \n    m_deleter(m_ptr); \n}\n \nclass Int\n{\npublic:\n\tInt() :x_(0) { std::cout &lt;&lt; \"creat Int\"; x_ += 1; }\n\tInt(int x) :x_(x) { std::cout &lt;&lt; \"creat Int\"; }\n\tInt(int x, int y):x_(x) { std::cout &lt;&lt; \"creat Int\"; }\n\tvoid Print()\n\t{\n\t\tstd::cout &lt;&lt; \"Int::\" &lt;&lt; x_;\n\t}\npublic:\n\tint x_;\n};<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u4e3a\u4e86\u6ee1\u8db3\u667a\u80fd\u6307\u9488\u7684\u552f\u4e00\u6027\uff0c\u4e0d\u5141\u8bb8\u62f7\u8d1d\u6784\u9020\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>My_unique_ptr(const My_unique_ptr&amp;) = delete;\nMy_unique_ptr&amp; operator=(const My_unique_ptr&amp;) = delete;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u63d0\u4f9b\u79fb\u52a8\u6784\u9020\u548c\u79fb\u52a8\u8d4b\u503c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>My_unique_ptr(My_unique_ptr&amp;&amp; other) :m_ptr(other.m_ptr) \n{\n     other.m_ptr = nullptr; \n}\n\t\nMy_unique_ptr&amp; operator=(My_unique_ptr&amp;&amp; other)\n\t\n{\n    if (this != &amp;other)\n    {\n        delete m_ptr;\n\tm_ptr = other.m_ptr;\n\tother.m_ptr = nullptr;\n    }\n    return *this;\n} \/\/xxxx<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. \u5b9e\u73b0\u5220\u9664\u5668<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>template&lt;class _Ty>\nstruct   Default_deleter\n{\n\tvoid operator() (_Ty* ptr)\n\t{\n\t\tdelete ptr;\n\t\tptr = nullptr;\n\t\tstd::cout &lt;&lt; \"destroyed\";\n\t}\n};\ntemplate&lt;class _Ty>\nstruct   Default_deleter&lt;_Ty&#91;]>\n{\n\tvoid operator ()(_Ty* ptr)\n\t{\n\t\tdelete&#91;]ptr;\n\t\tptr = nullptr;\n\t\tstd::cout &lt;&lt; \"destroyed \";\n\t}\n};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Shared_ptr<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0\u76ee\u6807<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b83\u53ef\u4ee5\u5728\u591a\u4e2a\u6307\u9488\u4e4b\u95f4\u5171\u4eab\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ece\u800c\u907f\u514d\u4e86\u5185\u5b58\u6cc4\u6f0f\u548c\u91ce\u6307\u9488\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li>shared_ptr \u7684\u5b9e\u73b0\u65b9\u5f0f\u901a\u5e38\u662f\u57fa\u4e8e\u5f15\u7528\u8ba1\u6570\u7684\u6280\u672f\uff0c\u5177\u4f53\u6765\u8bf4\uff0c\u6bcf\u4e2a shared_ptr \u5bf9\u8c61\u90fd\u4f1a\u7ef4\u62a4\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570\u5668\uff0c\u7528\u4e8e\u8bb0\u5f55\u6709\u591a\u5c11\u4e2a\u6307\u9488\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u5f53\u5f15\u7528\u8ba1\u6570\u5668\u4e3a 0 \u65f6\uff0c\u8868\u793a\u6ca1\u6709\u4efb\u4f55\u6307\u9488\u6307\u5411\u8be5\u5bf9\u8c61\uff0c\u6b64\u65f6\u4f1a\u81ea\u52a8\u91ca\u653e\u8be5\u5bf9\u8c61\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0\u7ec6\u8282<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;vector>\n#include &lt;unordered_set>\n\ntemplate &lt;typename T>\nclass shared_ptr {\nprivate:\n    T* ptr;\n    int* ref_count;\n    void release() {\n        if (ref_count) {\n            --(*ref_count);\n            if (*ref_count == 0) {\n                delete ptr;\n                delete ref_count;\n            }\n            ptr = nullptr;\n            ref_count = nullptr;\n        }\n    }\n\npublic:\n    shared_ptr() : ptr(nullptr), ref_count(nullptr) {}\n    shared_ptr(T* p) : ptr(p), ref_count(new int(1)) {}\n    shared_ptr(const shared_ptr&amp; other) : ptr(other.ptr), ref_count(other.ref_count) {\n        if (ref_count) {\n            ++(*ref_count);\n        }\n    }\n    ~shared_ptr() {\n        release();\n    }\n    shared_ptr&amp; operator=(const shared_ptr&amp; other) {\n        if (this != &amp;other) {\n            release();\n            ptr = other.ptr;\n            ref_count = other.ref_count;\n            if (ref_count) {\n                ++(*ref_count);\n            }\n        }\n        return *this;\n    }\n\n    T* get() const {\n        return ptr;\n    }\n\n    int use_count() const {\n        return ref_count ? *ref_count : 0;\n    }\n\n    void reset() {\n        release();\n    }\n\n    void reset(T* p) {\n        release();\n        ptr = p;\n        ref_count = new int(1);\n    }\n\n    T&amp; operator*() const {\n        return *ptr;\n    }\n\n    T* operator->() const {\n        return ptr;\n    }\n};\n\nclass MyClass {\npublic:\n    MyClass() {\n        std::cout &lt;&lt; \"MyClass constructor\" &lt;&lt; std::endl;\n    }\n    ~MyClass() {\n        std::cout &lt;&lt; \"MyClass destructor\" &lt;&lt; std::endl;\n    }\n    void hello() {\n        std::cout &lt;&lt; \"Hello, world!\" &lt;&lt; std::endl;\n    }\n};\n\nint main() {\n    shared_ptr&lt;MyClass> ptr1(new MyClass());\n    shared_ptr&lt;MyClass> ptr2 = ptr1;\n    std::cout &lt;&lt; \"ptr1 use count: \" &lt;&lt; ptr1.use_count() &lt;&lt; std::endl;\n    std::cout &lt;&lt; \"ptr2 use count: \" &lt;&lt; ptr2.use_count() &lt;&lt; std::endl;\n    ptr1->hello();\n    ptr2.reset();\n    std::cout &lt;&lt; \"ptr1 use count: \" &lt;&lt; ptr1.use_count() &lt;&lt; std::endl;\n    std::cout &lt;&lt; \"ptr2 use count: \" &lt;&lt; ptr2.use_count() &lt;&lt; std::endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u5f15\u7528\u8ba1\u6570\u5668<\/h4>\n\n\n\n<p>\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u5c06 ref_count \u521d\u59cb\u5316\u4e3a 1\uff0c\u8868\u793a\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u8be5\u5bf9\u8c61\u3002\u5728<strong>\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26<\/strong>\u4e2d\uff0c\u6211\u4eec\u5c06 ref_count \u7684\u503c\u52a0 1\uff0c\u8868\u793a\u6709\u4e00\u4e2a\u65b0\u7684\u6307\u9488\u6307\u5411\u8be5\u5bf9\u8c61\u3002\u5728\u6790\u6784\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u5c06 ref_count \u7684\u503c\u51cf 1\uff0c\u5982\u679c ref_count \u7684\u503c\u4e3a 0\uff0c\u5219\u8868\u793a\u6ca1\u6709\u4efb\u4f55\u6307\u9488\u6307\u5411\u8be5\u5bf9\u8c61\uff0c\u6b64\u65f6\u4f1a\u81ea\u52a8\u91ca\u653e\u8be5\u5bf9\u8c61\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u5176\u5b83\u7ec6\u8282<\/h4>\n\n\n\n<p>\u5728\u4f7f\u7528 shared_ptr \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 get \u51fd\u6570\u83b7\u53d6\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u901a\u8fc7 use_count \u51fd\u6570\u83b7\u53d6\u5f53\u524d\u6709\u591a\u5c11\u4e2a\u6307\u9488\u6307\u5411\u8be5\u5bf9\u8c61\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7 reset \u51fd\u6570\u91ca\u653e\u5f53\u524d\u6307\u9488\uff0c\u5e76\u91cd\u65b0\u6307\u5411\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7 operator* \u548c operator-> \u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u4f7f\u5f97 shared_ptr \u5bf9\u8c61\u53ef\u4ee5\u50cf\u666e\u901a\u6307\u9488\u4e00\u6837\u4f7f\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u4e3a\u4ec0\u4e48\u7528\u5f15\u7528\u8ba1\u6570\u800c\u4e0d\u7528static\u53d8\u91cf\u5462\uff1f<\/h4>\n\n\n\n<p>static \u53d8\u91cf\u53ef\u4ee5\u88ab\u591a\u4e2a\u53d8\u91cf\u5171\u4eab\uff0c\u4f46\u662f\u8fd9\u79cd\u5171\u4eab\u65b9\u5f0f\u662f\u57fa\u4e8e\u9759\u6001\u5b58\u50a8\u671f\u7684\uff0c\u800c\u4e0d\u662f\u57fa\u4e8e\u52a8\u6001\u5b58\u50a8\u671f\u7684\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5f53\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a static \u53d8\u91cf\u65f6\uff0c\u5b83\u4f1a\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u4e00\u76f4\u5b58\u5728\uff0c\u76f4\u5230\u7a0b\u5e8f\u7ed3\u675f\u624d\u4f1a\u88ab\u9500\u6bc1\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6240\u6709\u4f7f\u7528\u8be5 static \u53d8\u91cf\u7684\u53d8\u91cf\u90fd\u4f1a\u5171\u4eab\u540c\u4e00\u4e2a\u5185\u5b58\u7a7a\u95f4\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u591a\u53d8\u91cf\u5171\u4eab\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u7136\u800c\uff0c\u5728\u5b9e\u73b0 shared_ptr \u65f6\uff0c\u6211\u4eec\u9700\u8981\u7684\u662f<strong>\u52a8\u6001\u5b58\u50a8\u671f<\/strong>\u7684\u5171\u4eab\uff0c\u5373\u591a\u4e2a shared_ptr \u5bf9\u8c61\u4e4b\u95f4\u9700\u8981\u5171\u4eab\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u3002\u8fd9\u79cd\u5171\u4eab\u65b9\u5f0f\u662f\u57fa\u4e8e\u52a8\u6001\u5b58\u50a8\u671f\u7684\uff0c\u56e0\u4e3a shared_ptr \u5bf9\u8c61\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u52a8\u6001\u521b\u5efa\u548c\u9500\u6bc1\u7684\uff0c\u800c\u4e0d\u662f\u5728\u7a0b\u5e8f\u7f16\u8bd1\u671f\u95f4\u5c31\u786e\u5b9a\u7684\u3002\u56e0\u6b64\uff0c\u4f7f\u7528 static \u53d8\u91cf\u6765\u8bb0\u5f55\u5f15\u7528\u8ba1\u6570\u65e0\u6cd5\u5b9e\u73b0\u52a8\u6001\u5b58\u50a8\u671f\u7684\u5171\u4eab\uff0c\u4ece\u800c\u5bfc\u81f4\u5f15\u7528\u8ba1\u6570\u7684\u4e0d\u51c6\u786e\u548c\u5185\u5b58\u6cc4\u6f0f\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unique_ptr \u5b9e\u73b0\u76ee\u6807 \u5b9e\u73b0\u7ec6\u8282 1. \u663e\u793a\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570 2. \u4e3a\u4e86\u6ee1\u8db3\u667a\u80fd\u6307\u9488\u7684\u552f\u4e00\u6027\uff0c\u4e0d\u5141\u8bb8\u62f7\u8d1d\u6784\u9020\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-2431","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=2431"}],"version-history":[{"count":5,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2431\/revisions"}],"predecessor-version":[{"id":2436,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2431\/revisions\/2436"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=2431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=2431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=2431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}