{"id":179,"date":"2025-07-17T22:29:25","date_gmt":"2025-07-17T14:29:25","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=179"},"modified":"2025-11-24T16:41:05","modified_gmt":"2025-11-24T08:41:05","slug":"%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=179","title":{"rendered":"\u667a\u80fd\u6307\u9488"},"content":{"rendered":"\n<p>\u4ee5\u4e0b\u6d4b\u8bd5\u4ee3\u7801gitee\u4ed3\u5e93\u5730\u5740\uff1a<a href=\"https:\/\/gitee.com\/tgwTTT\/c-lreant\/tree\/master\/Smart%20pointer\">https:\/\/gitee.com\/tgwTTT\/c-lreant\/tree\/master\/Smart%20pointer<\/a><\/p>\n\n\n\n<p>\u5728C++\u4f7f\u7528\u5185\u5b58\u7684\u65f6\u5019\u5f88\u5bb9\u6613\u51fa\u73b0\u91ce\u6307\u9488\u3001\u7a7a\u6307\u9488\u3001\u5185\u5b58\u6cc4\u9732\u3002\u6240\u4ee5C++11\u5f15\u5165\u4e86\u667a\u80fd\u6307\u9488\u6765\u7ba1\u7406\u5185\u5b58\u3002\u5728c++11\u4e2d\u4e00\u5171\u5f15\u5165\u4e863\u79cd\u6307\u9488\uff0c\u5373unique_ptr \u3001shared_ptr\u3001weak_ptr.<\/p>\n\n\n\n<p>\u4e00.unique_ptr:\u4ed6\u7684\u7279\u70b9\u7684\u4e0d\u2f40\u6301\u62f7\u2ec9\uff0c\u53ea\u2f40\u6301\u79fb\u52a8\u3002\u5982\u679c\u4e0d\u9700\u8981\u62f7\u2ec9\u7684\u573a\u666f\u5c31\u2fae\u5e38\u5efa\u8bae\u4f7f\u2f64\u4ed6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Date\n{\n\tint _year;\n\tint _month;\n\tint _day;\n\tDate(int year = 1, int month = 1, int day = 1)\n\t\t:_year(year)\n\t\t, _month(month)\n\t\t, _day(day)\n\t{}\n\t~Date()\n\t{\n\t\tcout << \"~Date()\" << endl;\n\t}\n};\nunique_ptr<Date> up1(new Date);\n\/\/ \u4e0d\u2f40\u6301\u62f7\u2ec9\n\/\/unique_ptr<Date> up2(up1);\n\/\/ \u2f40\u6301\u79fb\u52a8\uff0c\u4f46\u662f\u79fb\u52a8\u540eup1\u4e5f\u60ac\u7a7a\uff0c\u6240\u4ee5\u4f7f\u2f64\u79fb\u52a8\u8981\u8c28\u614e\nunique_ptr<Date> up3(move(up1));<\/code><\/pre>\n\n\n\n<p>shared_unique:\u4ed6\u7684\u7279\u70b9\u662f\u2f40\u6301\u62f7\u2ec9\uff0c\u4e5f\u2f40\u6301\u79fb\u52a8\u3002\u5982\u679c\u9700\u8981\u62f7\u2ec9\u7684\u573a\u666f\u5c31\u9700\u8981\u4f7f\u2f64\u4ed6\u4e86\u3002\u5e95\u5c42\u662f\u2f64\u5f15\u2f64\u8ba1\u6570\u7684\u2f45\u5f0f\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<p>weak_ptr\uff1a\u4e0d\u80fd\u2f64\u5b83\u76f4\u63a5\u7ba1\u7406\u8d44\u6e90\uff0cweak_ptr\u7684\u4ea7\u2f63\u672c\u8d28\u662f\u8981\u89e3\u51b3shared_ptr\u7684\u2f00\u4e2a\u5faa\u73af\u5f15\u2f64\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u7684\u95ee\u9898\u3002\u5177\u4f53\u7ec6\u8282\u4e0b\u2faf\u6211\u4eec\u518d\u7ec6\u8bb2<\/p>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u4e86\u89e3\u4e00\u4e0bshared_unique\u7684\u5e95\u5c42\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace bit {\n\ttemplate<class T>\n\tclass shared_ptr\n\t{\n\t\tpublic :\n\t\texplicit shared_ptr(T* ptr)\n\t\t\t: _ptr(ptr)\n\t\t\t, _pcount(new int(1))\n\t\t{}\n\t\tshared_ptr(const shared_ptr<T>& sp)\/\/\u62f7\u8d1d\u6784\u9020\u8ba1\u6570++\n\t\t\t:_ptr(sp._ptr)\n\t\t\t, _pcount(sp._pcount)\n\t\t{\n\t\t\t++(*_pcount);\n\t\t} \n\t\t~shared_ptr()\n\t\t{\n\t\t\tcout << \"\u8ba1\u6570--\" << endl;\n\t\t\tif (--(*_pcount) == 0) {\n\t\t\t\t delete _ptr;\n\t\t\t\tdelete _pcount;\n\t\t\t}\n\t\t} \n\t\t\n\t\tT* get() const\n\t\t\t\n\t\t{\n\t\t\treturn _ptr;\n\t\t} \n\t\tint use_count() const\n\t\t{\n\t\t\treturn *_pcount;\n\t\t} \n\t\tT&#038; operator*()\n\t\t{\n\t\t\treturn *_ptr;\n\t\t} \n\t\tT* operator->()\n\t\t{\n\t\t\treturn _ptr;\n\t\t}\n\t\tshared_ptr<T>&operator=(const shared_ptr<T>&sp) \n\t\t{\n\t\t\tif (sp._ptr != _ptr) {\n\t\t\t\tif (--(*_pcount) == 0) {\n\t\t\t\t\tcout << \"\u8ba1\u6570--\" << endl;\n\t\t\t\t\tdelete _ptr;\n\n\t\t\t\t\tdelete _pcount;\n\t\t\t\t}\n\t\t\t\t_pcount = sp._pcount;\n\t\t\t\t_ptr = sp._ptr;\n\t\t\t\t++(*_pcount);\n\t\t\t}\n\t\t\treturn *this;\n\t\t}\n\tprivate:\n\t\tT* _ptr;\n\t\tint* _pcount;\n\t};\n}\n\nshared_ptr\uff1a\u7684\u5e95\u5c42\u539f\u7406\u662f\u5229\u7528\u4e00\u4e2apcount\u8ba1\u6570\u6307\u9488\uff0c\u6bcf\u5f53\u51fd\u6570\u8fdb\u884c\u62f7\u8d1d\u6784\u9020\u6216\u8d4b\u503c\u65f6\u5c31++,\u5f53pcount==0,\u5373\u6240\u6709\u6307\u5411\u5730\u5740\u7684\u6307\u9488\u88ab\u6790\u6784\u65f6,pcount--.\u76f4\u5230\u96f6\u65f6delete pcount \u548cptr\u6307\u9488\u3002\n\u4f46\u662f\u6b64\u65f6\u6211\u4eec\u4f1a\u51fa\u73b0\u4e00\u4e2a\u65b0\u7684\u95ee\u9898\uff0c\u6211\u4eec\u65e0\u6cd5delete \u90a3\u4e9b\u7279\u6b8a\u7c7b\u5982\uff1a[],file\u7b49\uff0c\u6b64\u65f6\u6211\u4eec\u5c31\u5fc5\u987b\u518d\u589e\u52a0\u4e00\u4e2a\u51fd\u6570\u53bb\u4fdd\u8bc1\u4ed6\u80fd\u6790\u6784\u6210\u529f\uff0c\u6b64\u65f6\u6211\u4eec\u5c31\u5fc5\u987b\u5f15\u5165\u5982\u4e0b\u4ee3\u7801\uff1a\n\u5728\u7c7b\u91cc\u9762\u6dfb\u52a0\uff1a\ntemplate<class D>\nshared_ptr(T* ptr, D del) \n\t: _ptr(ptr)\n\t, _pcount(new int(1))\n\t,_del(del)\n{}\n\u518d\u5229\u7528functional\u7c7b\u91cc\u9762\u7684\u5c01\u88c5\u4e00\u4e2a\u5bf9\u8c61void(T*)\uff1a\nfunction<void(T*)> _del = [](T* ptr) {\n\tdelete[] ptr;\n};\n\u6b64\u65f6\u4ee3\u7801\u5c31\u8d8b\u8fd1\u4e8e\u5b8c\u5584\u3002<\/code><\/pre>\n\n\n\n<p>\u6709\u4e86\u4ee3\u7801\u540e\u6211\u4eec\u56de\u5230\u4e0a\u9762\u7684\u95ee\u9898\u5faa\u73af\u5f15\u7528\uff1a\u5f53\u6211\u4eec\u6709\u4e24\u4e2a\u6709\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u7c7b\u65f6\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"798\" height=\"515\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-33.png\" alt=\"\" class=\"wp-image-180\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-33.png 798w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-33-300x194.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-33-768x496.png 768w\" sizes=\"auto, (max-width: 798px) 100vw, 798px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"74\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-34.png\" alt=\"\" class=\"wp-image-181\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-34.png 700w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-34-300x32.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>1. \u53f3\u8fb9\u7684\u8282\u70b9\u4ec0\u4e48\u65f6\u5019\u91ca\u653e\u5462\uff0c\u5de6\u8fb9\u8282\u70b9\u4e2d\u7684_next\u7ba1\u7740\u5462\uff0c_next\u6790\u6784\u540e\uff0c\u53f3\u8fb9\u7684\u8282\u70b9\u5c31\u91ca\u653e\u4e86\u3002 \n2. _next\u4ec0\u4e48\u65f6\u5019\u6790\u6784\u5462\uff0c_next\u662f\u5de6\u8fb9\u8282\u70b9\u7684\u7684\u6210\u5458\uff0c\u5de6\u8fb9\u8282\u70b9\u91ca\u653e\uff0c_next\u5c31\u6790\u6784\u4e86\u3002 \n3. \u5de6\u8fb9\u8282\u70b9\u4ec0\u4e48\u65f6\u5019\u91ca\u653e\u5462\uff0c\u5de6\u8fb9\u8282\u70b9\u7531\u53f3\u8fb9\u8282\u70b9\u4e2d\u7684_prev\u7ba1\u7740\u5462\uff0c_prev\u6790\u6784\u540e\uff0c\u5de6\u8fb9\u7684\u8282\u70b9\u5c31\u91ca\n\u653e\u4e86\u3002\n4. _prev\u4ec0\u4e48\u65f6\u5019\u6790\u6784\u5462\uff0c_prev\u662f\u53f3\u8fb9\u8282\u70b9\u7684\u6210\u5458\uff0c\u53f3\u8fb9\u8282\u70b9\u91ca\u653e\uff0c_prev\u5c31\u6790\u6784\u4e86\u3002 \n\u2f84\u6b64\u903b\u8f91\u4e0a\u6210\u529f\u5f62\u6210\u56de\u65cb\u9556\u4f3c\u7684\u5faa\u73af\u5f15\u2f64\uff0c\u8c01\u90fd\u4e0d\u4f1a\u91ca\u653e\u5c31\u5f62\u6210\u4e86\u5faa\u73af\u5f15\u2f64\uff0c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\n\u628aListNode\u7ed3\u6784\u4f53\u4e2d\u7684_next\u548c_prev\u6539\u6210weak_ptr\uff0cweak_ptr\u7ed1\u5b9a\u5230shared_ptr\u65f6\u4e0d\u4f1a\u589e\u52a0\u5b83\u7684\n\u5f15\u2f64\u8ba1\u6570\uff0c_next\u548c_prev\u4e0d\u53c2\u4e0e\u8d44\u6e90\u91ca\u653e\u7ba1\u7406\u903b\u8f91\uff0c\u5c31\u6210\u529f\u6253\u7834\u4e86\u5faa\u73af\u5f15\u2f64\uff0c\u89e3\u51b3\u4e86\u8fd9\u2fa5\u7684\u95ee\u9898<\/code><\/pre>\n\n\n\n<p>weak_ptr:weak_ptr\u6784\u9020\u65f6\u4e0d\u2f40\u6301\u7ed1\u5b9a\u5230\u8d44\u6e90\uff0c\u53ea\u2f40\u6301\u7ed1\u5b9a\u5230shared_ptr\uff0c\u7ed1\u5b9a\u5230shared_ptr\u65f6\uff0c\u4e0d\u589e\u52a0shared_ptr\u7684\u5f15\u2f64\u8ba1\u6570\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u89e3\u51b3\u4e0a\u8ff0\u7684\u5faa\u73af\u5f15\u2f64\u95ee\u9898\u3002weak_ptr\u4e5f\u6ca1\u6709\u91cd\u8f7doperator*\u548coperator->\u7b49\uff0c\u56e0\u4e3a\u4ed6\u4e0d\u53c2\u4e0e\u8d44\u6e90\u7ba1\u7406\uff0c\u90a3\u4e48\u5982\u679c\u4ed6\u7ed1\u5b9a\u7684shared_ptr\u5df2\u7ecf\u91ca\u653e\u4e86\u8d44\u6e90\uff0c\u90a3\u4e48\u4ed6\u53bb\u8bbf\u95ee\u8d44\u6e90\u5c31\u662f\u5f88\u5371\u9669\u7684\u3002weak_ptr\u2f40\u6301expired\u68c0\u67e5\u6307\u5411\u7684\u8d44\u6e90\u662f\u5426\u8fc7\u671f\uff0cuse_count\u4e5f\u53ef\u83b7\u53d6shared_ptr\u7684\u5f15\u2f64\u8ba1\u6570\uff0cweak_ptr\u60f3\u8bbf\u95ee\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u8c03\u2f64lock\u8fd4\u56de\u2f00\u4e2a\u7ba1\u7406\u8d44\u6e90\u7684shared_ptr\uff0c\u5982\u679c\u8d44\u6e90\u5df2\u7ecf\u88ab\u91ca\u653e\uff0c\u8fd4\u56de\u7684shared_ptr\u662f\u2f00\u4e2a\u7a7a\u5bf9\u8c61\uff0c\u5982\u679c\u8d44\u6e90\u6ca1\u6709\u91ca\u653e\uff0c\u5219\u901a\u8fc7\u8fd4\u56de\u7684shared_ptr\u8bbf\u95ee\u8d44\u6e90\u662f\u5b89\u5168\u7684,\u4e0b\u9762\u662f\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main() {\n\tstd::shared_ptr<string> sp1(new string(\"111111\"));\n\tstd::shared_ptr<string> sp2(sp1);\n\tstd::weak_ptr<string> wp = sp1;\n\tcout << wp.expired() << endl;\n\tcout << wp.use_count() << endl;\n\t\/\/ sp1\u548csp2\u90fd\u6307\u5411\u4e86\u5176\u4ed6\u8d44\u6e90\uff0c\u5219weak_ptr\u5c31\u8fc7\u671f\u4e86\n\tsp1 = make_shared<string>(\"222222\");\n\tcout << \"2222222\" << endl;\n\tcout << wp.expired() << endl;\n\tcout << wp.use_count() << endl;\n\tcout << \"3333333\" << endl;\n\tsp2 = make_shared<string>(\"333333\");\n\tcout << wp.expired() << endl;\n\tcout << wp.use_count() << endl;\n\twp = sp1;\n\t\/\/std::shared_ptr<string> sp3 = wp.lock();\n\tauto sp3 = wp.lock();\n\tcout << wp.expired() << endl;\n\tcout << wp.use_count() << endl;\n\t*sp3 += \"###\";\n    cout << *sp1 << endl;\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"149\" height=\"261\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-35.png\" alt=\"\" class=\"wp-image-182\"\/><\/figure>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\u4e86\uff0c\u5982\u6709\u9519\u8bef\uff0c\u6b22\u8fce\u6307\u51fa\uff01\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u6d4b\u8bd5\u4ee3\u7801gitee\u4ed3\u5e93\u5730\u5740\uff1ahttps:\/\/gitee.com\/tgwTTT\/c-lreant\/tree\/&#8230;<\/p>\n","protected":false},"author":1,"featured_media":181,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-179","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=179"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":870,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions\/870"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/181"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}