{"id":40962,"date":"2025-05-30T12:26:59","date_gmt":"2025-05-30T04:26:59","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/40962.html"},"modified":"2025-05-30T12:26:59","modified_gmt":"2025-05-30T04:26:59","slug":"%e3%80%90c%e9%ab%98%e9%98%b6%e3%80%91%ef%bc%9a%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88%e7%9a%84%e5%85%a8%e9%9d%a2%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/40962.html","title":{"rendered":"\u3010C++\u9ad8\u9636\u3011\uff1a\u667a\u80fd\u6307\u9488\u7684\u5168\u9762\u89e3\u6790"},"content":{"rendered":"<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"540\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042655-6839338f8b729.jpg\" width=\"960\" \/><\/p>\n<p>\u2728\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<span style=\"color:#4da8ee\">\u00a0 \u00a0 \u843d\u7d6e\u65e0\u58f0\u6625\u5815\u6cea&#xff0c;\u884c\u4e91\u6709\u5f71\u6708\u542b\u7f9e\u00a0<\/span> \u00a0 \u00a0&#x1f30f;<\/p>\n<p>&#x1f4c3;\u4e2a\u4eba\u4e3b\u9875&#xff1a;island1314<\/p>\n<p>&#x1f525;\u4e2a\u4eba\u4e13\u680f&#xff1a;C&#043;&#043;\u5b66\u4e60<\/p>\n<p>&#x1f680; \u6b22\u8fce\u5173\u6ce8&#xff1a;&#x1f44d;\u70b9\u8d5e &#x1f442;&#x1f3fd;\u7559\u8a00 &#x1f60d;\u6536\u85cf \u00a0&#x1f49e; &#x1f49e; &#x1f49e;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042655-6839338fb5057.gif\" \/><\/p>\n<hr \/>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"1.%E5%BC%95%E8%A8%80-toc\" style=\"margin-left:40px\">1.\u5f15\u8a00<\/p>\n<p id=\"%C2%A01.1%20%E5%8A%A8%E6%80%81%E5%86%85%E5%AD%98%E4%B8%8E%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88-toc\" style=\"margin-left:80px\">\u00a01.1 \u52a8\u6001\u5185\u5b58\u4e0e\u667a\u80fd\u6307\u9488<\/p>\n<p id=\"1.1.1%20%E5%8A%A8%E6%80%81%E5%86%85%E5%AD%98-toc\" style=\"margin-left:120px\">&#x1f338;1.1.1 \u52a8\u6001\u5185\u5b58<\/p>\n<p id=\"1.1.2%C2%A0%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88-toc\" style=\"margin-left:120px\">&#x1f338;1.1.2\u00a0\u667a\u80fd\u6307\u9488<\/p>\n<p id=\"1.2%20%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E7%9A%84%E9%87%8D%E8%A6%81%E6%80%A7-toc\" style=\"margin-left:80px\">1.2 \u667a\u80fd\u6307\u9488\u7684\u91cd\u8981\u6027<\/p>\n<p id=\"1.3%20%E5%83%8F%E6%8C%87%E9%92%88%E4%B8%80%E6%A0%B7%E4%BD%BF%E7%94%A8-toc\" style=\"margin-left:80px\">1.3 \u50cf\u6307\u9488\u4e00\u6837\u4f7f\u7528<\/p>\n<p id=\"1.4%C2%A0%E6%94%AF%E6%8C%81%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E5%AF%B9%E8%B1%A1%E6%8B%B7%E8%B4%9D-toc\" style=\"margin-left:80px\">1.4\u00a0\u652f\u6301\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u62f7\u8d1d<\/p>\n<p id=\"2.%20C%2B%2B%E6%A0%87%E5%87%86%E5%BA%93%E7%9A%84%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88-toc\" style=\"margin-left:40px\">2. C&#043;&#043;\u6807\u51c6\u5e93\u7684\u667a\u80fd\u6307\u9488<\/p>\n<p id=\"2.1%20auto_ptr-toc\" style=\"margin-left:80px\">2.1 auto_ptr<\/p>\n<p id=\"2.1.1%20%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-toc\" style=\"margin-left:120px\">&#x1f308;2.1.1 \u57fa\u672c\u6982\u5ff5<\/p>\n<p id=\"2.1.2%C2%A0%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95-toc\" style=\"margin-left:120px\">&#x1f308;2.1.2\u00a0\u57fa\u672c\u7528\u6cd5<\/p>\n<p id=\"2.2%C2%A0unique_ptr-toc\" style=\"margin-left:80px\">2.2\u00a0unique_ptr<\/p>\n<p id=\"2.2.1%20%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-toc\" style=\"margin-left:120px\">\u27282.2.1 \u57fa\u672c\u6982\u5ff5<\/p>\n<p id=\"2.2.1%20%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95-toc\" style=\"margin-left:120px\">\u27282.2.2\u00a0\u57fa\u672c\u7528\u6cd5<\/p>\n<p id=\"2.3%C2%A0shared_ptr-toc\" style=\"margin-left:80px\">2.3\u00a0shared_ptr<\/p>\n<p id=\"2.3.1%20%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5%E5%8F%8A%E5%8E%9F%E7%90%86-toc\" style=\"margin-left:120px\">&#x1f388;2.3.1 \u57fa\u672c\u6982\u5ff5<\/p>\n<p id=\"2.3.2%20%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95-toc\" style=\"margin-left:120px\">&#x1f388;2.3.2 \u57fa\u672c\u7528\u6cd5<\/p>\n<p id=\"2.3.3%20%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9-toc\" style=\"margin-left:120px\">&#x1f388;2.3.3 \u6ce8\u610f\u4e8b\u9879<\/p>\n<p id=\"2.4%C2%A0weak_ptr-toc\" style=\"margin-left:80px\">2.4\u00a0weak_ptr<\/p>\n<p id=\"2.4.1%20%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-toc\" style=\"margin-left:120px\">&#x1f9e9;2.4.1 \u57fa\u672c\u6982\u5ff5<\/p>\n<p id=\"2.4.2%20%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95-toc\" style=\"margin-left:120px\">&#x1f9e9;2.4.2 \u57fa\u672c\u7528\u6cd5<\/p>\n<p id=\"2.4.3%20%E5%AF%B9%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8%E7%9A%84%E8%A7%A3%E5%86%B3-toc\" style=\"margin-left:120px\">&#x1f9e9;2.4.3 \u5bf9\u5faa\u73af\u5f15\u7528\u7684\u89e3\u51b3<\/p>\n<p id=\"2.4.4%20%E4%B8%8Eshared_ptr%E7%9A%84%E5%85%B3%E7%B3%BB-toc\" style=\"margin-left:120px\">&#x1f9e9;2.4.4 \u4e0eshared_ptr\u7684\u5173\u7cfb<\/p>\n<p id=\"3.%20%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E7%9A%84%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:40px\">3. \u667a\u80fd\u6307\u9488\u7684\u5b9e\u73b0<\/p>\n<p id=\"3.1%20auto_ptr%E6%A8%A1%E6%8B%9F%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:80px\">3.1 auto_ptr\u6a21\u62df\u5b9e\u73b0<\/p>\n<p id=\"3.2%C2%A0unique_ptr%E6%A8%A1%E6%8B%9F%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:80px\">3.2\u00a0unique_ptr\u6a21\u62df\u5b9e\u73b0<\/p>\n<p id=\"3.3%C2%A0shared_ptr%E6%A8%A1%E6%8B%9F%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:80px\">3.3\u00a0shared_ptr\u6a21\u62df\u5b9e\u73b0<\/p>\n<p id=\"3.4%C2%A0weak_ptr%E6%A8%A1%E6%8B%9F%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:80px\">3.4\u00a0weak_ptr\u6a21\u62df\u5b9e\u73b0<\/p>\n<p id=\"%F0%9F%93%96%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:40px\">&#x1f4d6;\u603b\u7ed3<\/p>\n<\/p>\n<hr id=\"hr-toc\" \/>\n<h3 id=\"1.%E5%BC%95%E8%A8%80\">1.\u5f15\u8a00<\/h3>\n<p><span style=\"color:#956fe7\">\u5230\u76ee\u524d\u4e3a\u6b62&#xff0c;\u6211\u4eec\u7f16\u5199\u7684\u7a0b\u5e8f\u4e2d\u6240\u4f7f\u7528\u7684\u5bf9\u8c61\u90fd\u6709\u7740\u4e25\u683c\u5b9a\u4e49\u7684\u751f\u5b58\u671f&#xff1a;<\/span><\/p>\n<ul>\n<li>\u5168\u5c40\u5bf9\u8c61&#xff1a;\u7a0b\u5e8f\u542f\u52a8\u65f6\u5206\u914d&#xff0c;\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002<\/li>\n<li>\u5c40\u90e8\u5bf9\u8c61&#xff1a;\u5f53\u6211\u4eec\u8fdb\u5165\u5176\u5b9a\u4e49\u6240\u5728\u7684\u7a0b\u5e8f\u5757\u65f6\u88ab\u521b\u5efa&#xff0c;\u5728\u79bb\u5f00\u5757\u65f6\u9500\u6bc1\u3002<\/li>\n<li>\u5c40\u90e8static\u5bf9\u8c61&#xff1a;\u5728\u7b2c\u4e00\u6b21\u4f7f\u7528\u524d\u5206\u914d&#xff0c;\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#956fe7\">\u6211\u4eec\u7684\u7a0b\u5e8f\u5230\u76ee\u524d\u4e3a\u6b62\u53ea\u4f7f\u7528\u8fc7\u9759\u6001\u5185\u5b58\u6216\u6808\u5185\u5b58&#xff1a;<\/span><\/p>\n<ul>\n<li>\u9759\u6001\u5185\u5b58&#xff1a;\u4fdd\u5b58\u5c40\u90e8static\u5bf9\u8c61\u3001\u7c7bstatic\u6570\u636e\u6210\u5458\u4ee5\u53ca\u5b9a\u4e49\u5728\u4efb\u4f55\u51fd\u6570\u4e4b\u5916\u7684\u53d8\u91cf\u3002<\/li>\n<li>\u6808\u5185\u5b58&#xff1a;\u4fdd\u5b58\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u7684\u975estatic\u5bf9\u8c61\u3002\u5206\u914d\u5728\u9759\u6001\u6216\u6808\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u521b\u5efa\u548c\u9500\u6bc1\u3002\u5bf9\u4e8e\u6808\u5bf9\u8c61&#xff0c;\u4ec5\u5728\u5176\u5b9a\u4e49\u7684\u7a0b\u5e8f\u5757\u8fd0\u884c\u65f6\u624d\u5b58\u5728&#xff1b;static\u5bf9\u8c61\u5728\u4f7f\u7528\u4e4b\u524d\u5206\u914d&#xff0c;\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002<\/li>\n<\/ul>\n<p>\u00a0 &#x1f4d2;\u9664\u4e86\u9759\u6001\u5185\u5b58\u548c\u6808\u5185\u5b58&#xff0c;\u6bcf\u4e2a\u7a0b\u5e8f\u8fd8\u62e5\u6709\u4e00\u4e2a\u5185\u5b58\u6c60\u3002\u8fd9\u90e8\u5206\u5185\u5b58\u88ab\u79f0\u4f5c\u81ea\u7531\u7a7a\u95f4&#xff08;free store&#xff09;\u6216\u5806&#xff08;heap&#xff09;\u3002\u7a0b\u5e8f\u7528\u5806\u6765\u5b58\u50a8\u52a8\u6001\u5206\u914d&#xff08;dynamically allocate&#xff09;\u7684\u5bf9\u8c61\u3002\u52a8\u6001\u5bf9\u8c61\u7684\u751f\u5b58\u671f\u7531\u7a0b\u5e8f\u6765\u63a7\u5236&#xff0c;\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u5f53\u52a8\u6001\u5bf9\u8c61\u4e0d\u518d\u4f7f\u7528\u65f6&#xff0c;\u6211\u4eec\u7684\u4ee3\u7801\u5fc5\u987b\u663e\u5f0f\u5730\u9500\u6bc1\u5b83\u4eec\u3002<\/p>\n<\/p>\n<h4 id=\"%C2%A01.1%20%E5%8A%A8%E6%80%81%E5%86%85%E5%AD%98%E4%B8%8E%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88\">\u00a01.1 \u52a8\u6001\u5185\u5b58\u4e0e\u667a\u80fd\u6307\u9488<\/h4>\n<h5 id=\"1.1.1%20%E5%8A%A8%E6%80%81%E5%86%85%E5%AD%98\">&#x1f338;1.1.1 \u52a8\u6001\u5185\u5b58<\/h5>\n<p>\u00a0 &#x1f4d2;\u9664\u4e86\u5c40\u90e8\u548cstatic\u5bf9\u8c61\u5916&#xff0c;C&#043;&#043;\u8fd8\u652f\u6301\u52a8\u6001\u5206\u914d\u5bf9\u8c61\u3002\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\u7684\u751f\u5b58\u671f\u4e0e\u5b83\u4eec\u5728\u54ea\u91cc\u521b\u5efa\u662f\u65e0\u5173\u7684&#xff0c;\u53ea\u6709\u5f53\u663e\u5f0f\u5730\u88ab\u91ca\u653e\u65f6&#xff0c;\u8fd9\u4e9b\u5bf9\u8c61\u624d\u4f1a\u9500\u6bc1\u3002\u52a8\u6001\u5bf9\u8c61\u7684\u6b63\u786e\u91ca\u653e\u662f\u7f16\u7a0b\u4e2d\u6781\u5176\u5bb9\u6613\u51fa\u9519\u7684\u5730\u65b9\u3002\u4e3a\u4e86\u66f4\u5b89\u5168\u5730\u4f7f\u7528\u52a8\u6001\u5bf9\u8c61&#xff0c;C&#043;&#043;\u6807\u51c6\u5e93\u5b9a\u4e49\u4e86\u4e24\u4e2a\u667a\u80fd\u6307\u9488\u7c7b\u578b\u6765\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\u3002\u5f53\u4e00\u4e2a\u5bf9\u8c61\u5e94\u8be5\u88ab\u91ca\u653e\u65f6&#xff0c;\u6307\u5411\u5b83\u7684\u667a\u80fd\u6307\u9488\u53ef\u4ee5\u786e\u4fdd\u81ea\u52a8\u5730\u91ca\u653e\u5b83\u3002<\/p>\n<ul>\n<li>new&#xff1a;\u5728\u52a8\u6001\u5185\u5b58\u4e2d\u4e3a\u5bf9\u8c61\u5206\u914d\u7a7a\u95f4\u5e76\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8be5\u5bf9\u8c61\u7684\u6307\u9488&#xff0c;\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u5bf9\u5bf9\u8c61\u8fdb\u884c\u521d\u59cb\u5316&#xff1b;<\/li>\n<li>delete&#xff1a;\u5728\u53d7\u4e00\u4e2a\u52a8\u6001\u5bf9\u8c61\u7684\u6307\u9488&#xff0c;\u9500\u6bc1\u8be5\u5bf9\u8c61&#xff0c;\u5e76\u91ca\u653e\u4e0e\u4e4b\u5173\u8054\u7684\u5185\u5b58\u3002<\/li>\n<\/ul>\n<p>\u4f46\u662f\u52a8\u6001\u5185\u5b58\u7684\u4f7f\u7528\u5f88\u5bb9\u6613\u51fa\u95ee\u9898&#xff1b;<\/p>\n<ul>\n<li>\u5185\u5b58\u6cc4\u6f0f&#xff1a;\u6709\u65f6\u6211\u4eec\u4f1a\u5fd8\u8bb0\u91ca\u653e\u5185\u5b58&#xff0c;\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u4f1a\u4ea7\u751f\u5185\u5b58\u6cc4\u6f0f&#xff1b;<\/li>\n<li>\u975e\u6cd5\u6307\u9488&#xff1a;\u6709\u65f6\u5728\u5c1a\u6709\u6307\u9488\u5f15\u7528\u5185\u5b58\u7684\u60c5\u51b5\u4e0b\u6211\u4eec\u5c31\u91ca\u653e\u4e86\u5b83&#xff0c;\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u4f1a\u4ea7\u751f\u5f15\u7528\u975e\u6cd5\u5185\u5b58\u7684\u6307\u9488\u3002<\/li>\n<\/ul>\n<p>\u00a0 &#x1f4d2;C&#043;&#043;11\u4e3a\u4e86\u66f4\u5bb9\u6613&#xff08;\u540c\u65f6\u4e5f\u66f4\u5b89\u5168&#xff09;\u5730\u4f7f\u7528\u52a8\u6001\u5185\u5b58&#xff0c;\u65b0\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u667a\u80fd\u6307\u9488&#xff08;smart pointer&#xff09;\u7c7b\u578b\u6765\u7ba1\u7406\u52a8\u6001\u5bf9\u8c61\u3002\u667a\u80fd\u6307\u9488\u7684\u884c\u4e3a\u7c7b\u4f3c\u5e38\u89c4\u6307\u9488&#xff0c;\u91cd\u8981\u7684\u533a\u522b\u662f\u5b83\u8d1f\u8d23\u81ea\u52a8\u91ca\u653e\u6240\u6307\u5411\u7684\u5bf9\u8c61\u3002\u5177\u4f53\u8bf4\u660e\u5982\u4e0b&#xff1a;<\/p>\n<h5 id=\"1.1.2%C2%A0%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88\">&#x1f338;1.1.2\u00a0\u667a\u80fd\u6307\u9488<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4ece\u6bd4\u8f83\u7b80\u5355\u7684\u5c42\u9762\u6765\u770b&#xff0c;\u667a\u80fd\u6307\u9488\u662f<span style=\"color:#956fe7\">\u00a0<\/span><span style=\"color:#956fe7\">RAII(Resource Acquisition Is Initialization&#xff0c;\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316)\u00a0<\/span>\u673a\u5236\u5bf9\u666e\u901a\u6307\u9488\u8fdb\u884c\u7684\u4e00\u5c42\u5c01\u88c5&#xff0c;\u5229\u7528\u5bf9\u8c61\u751f\u547d\u5468\u671f\u6765\u63a7\u5236\u7a0b\u5e8f\u8d44\u6e90&#xff08;\u5982\u5185\u5b58\u3001\u6587\u4ef6\u53e5\u67c4\u3001\u7f51\u7edc\u8fde\u63a5\u3001\u4e92\u65a5\u91cf\u7b49\u7b49&#xff09;\u7684\u7b80\u5355\u6280\u672f\u3002\u8fd9\u6837\u4f7f\u5f97\u667a\u80fd\u6307\u9488\u7684\u884c\u4e3a\u52a8\u4f5c\u50cf\u4e00\u4e2a\u6307\u9488&#xff0c;\u672c\u8d28\u4e0a\u5374\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u7ba1\u7406\u4e00\u4e2a\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<ul>\n<li>\u667a\u80fd\u6307\u9488\u672c\u8eab&#xff1a;\u00a0\u667a\u80fd\u6307\u9488\u662f\u4e00\u4e2a\u7c7b\u6a21\u677f\u7684\u5b9e\u4f8b&#xff0c;\u901a\u5e38\u4f5c\u4e3a\u5c40\u90e8\u53d8\u91cf\u5b58\u5728\u4e8e\u6808\u533a&#xff08;Stack&#xff09;\u3002\u5f53\u51fd\u6570\u8fd4\u56de\u6216\u8005\u5c40\u90e8\u53d8\u91cf\u8d85\u51fa\u5176\u4f5c\u7528\u57df\u65f6&#xff0c;\u6808\u533a\u7684\u5185\u5b58\u4f1a\u88ab\u81ea\u52a8\u91ca\u653e\u3002<\/li>\n<li>\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5bf9\u8c61&#xff1a;\u00a0\u667a\u80fd\u6307\u9488\u901a\u5e38\u7528\u6765\u7ba1\u7406\u5728\u5806\u533a&#xff08;Heap&#xff09;\u4e0a\u5206\u914d\u7684\u5185\u5b58\u3002\u8fd9\u662f\u901a\u8fc7\u8c03\u7528\u5982new\u64cd\u4f5c\u7b26\u6765\u5b8c\u6210\u7684\u3002\u5806\u533a\u7684\u5185\u5b58\u4f1a\u4e00\u76f4\u5b58\u5728&#xff0c;\u76f4\u5230\u663e\u5f0f\u5730\u91ca\u653e\u5b83&#xff08;\u4f7f\u7528delete\u64cd\u4f5c\u7b26&#xff09;\u6216\u8005\u5f53\u7a0b\u5e8f\u7ed3\u675f\u65f6\u624d\u4f1a\u88ab\u7cfb\u7edf\u56de\u6536\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\">\u5176\u5b9e\u8d28\u5982\u4e0b&#xff1a;<\/span><\/p>\n<ul>\n<li>\u00a0\u7531\u4e8eC&#043;&#043;\u4e2d\u6ca1\u6709\u5783\u573e\u56de\u6536\u673a\u5236&#xff0c;\u5fc5\u987b\u81ea\u5df1\u91ca\u653e\u6389\u5206\u914d\u7684\u5185\u5b58&#xff0c;\u5426\u5219\u5c31\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u9732\u3002\u6bd4\u5982\u5bf9\u4e8e\u666e\u901a\u7684new\u6216\u8005malloc\u5206\u914d\u7a7a\u95f4&#xff0c;\u9700\u8981\u53ca\u65f6\u7684\u91ca\u653e\u7a7a\u95f4&#xff0c;\u5426\u5219\u5bb9\u6613\u9020\u6210\u5185\u5b58\u6cc4\u6f0f&#xff0c;\u5bfc\u81f4\u5806\u533a\u7a7a\u95f4\u4e0d\u8db3\u3002\u4f46\u662f&#xff0c;\u968f\u7740\u4ee3\u7801\u91cf\u7684\u589e\u591a&#xff0c;\u5f88\u5bb9\u6613\u5fd8\u8bb0\u91ca\u653e\u7a7a\u95f4&#xff0c;\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u667a\u80fd\u6307\u9488&#xff08;smart pointer&#xff09;&#xff0c;\u5176\u4f1a\u81ea\u52a8\u91ca\u653e\u3002\u7701\u53bb\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u3002<\/li>\n<li>\u667a\u80fd\u6307\u9488\u662f\u5b58\u50a8\u6307\u5411\u52a8\u6001\u5206\u914d&#xff08;\u5806&#xff09;\u5bf9\u8c61\u6307\u9488\u7684\u7c7b&#xff0c;\u7528\u4e8e\u751f\u5b58\u671f\u7684\u63a7\u5236&#xff0c;\u80fd\u591f\u786e\u4fdd\u5728\u79bb\u5f00\u6307\u9488\u6240\u5728\u4f5c\u7528\u57df\u65f6&#xff0c;\u81ea\u52a8\u5730\u9500\u6bc1\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61&#xff0c;\u9632\u6b62\u5185\u5b58\u6cc4\u9732\u3002\u667a\u80fd\u6307\u9488\u7684\u6838\u5fc3\u5b9e\u73b0\u6280\u672f\u662f<span style=\"color:#fe2c24\">\u5f15\u7528\u8ba1\u6570<\/span>&#xff0c;\u6bcf\u4f7f\u7528\u5b83\u4e00\u6b21&#xff0c;\u5185\u90e8\u5f15\u7528\u8ba1\u6570\u52a01&#xff0c;\u8ba1\u6570\u5668\u7684\u5927\u5c0f\u7b49\u4e8e\u6307\u5411\u5bf9\u8c61\u7684\u667a\u80fd\u6307\u9488\u7684\u6570\u91cf&#xff0c;\u6bcf\u6790\u6784\u4e00\u6b21\u5185\u90e8\u7684\u5f15\u7528\u8ba1\u6570\u51cf1&#xff0c;\u51cf\u4e3a0\u65f6&#xff0c;\u5220\u9664\u6240\u6307\u5411\u7684\u5806\u5185\u5b58\u3002<\/li>\n<\/ul>\n<p>\u6ce8\u610f&#xff1a;\u667a\u80fd\u6307\u9488\u7684\u5934\u6587\u4ef6\u4e3a<span style=\"color:#be191c\">&lt;memory&gt;<\/span><\/p>\n<h4 id=\"1.2%20%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E7%9A%84%E9%87%8D%E8%A6%81%E6%80%A7\">1.2 \u667a\u80fd\u6307\u9488\u7684\u91cd\u8981\u6027<\/h4>\n<p>\u00a0 &#x1f4d2;\u5728C&#043;&#043;\u7f16\u7a0b\u4e2d&#xff0c;\u5185\u5b58\u7ba1\u7406\u4e00\u76f4\u662f\u4e00\u4e2a\u4e0d\u53ef\u6216\u7f3a\u7684\u8bdd\u9898\u3002\u4f20\u7edf\u7684C&#043;&#043;\u7a0b\u5e8f\u5458\u4f9d\u8d56new\u548cdelete&#xff08;\u65b0\u5efa\u548c\u5220\u9664&#xff09;\u6765\u624b\u52a8\u7ba1\u7406\u5185\u5b58&#xff0c;\u4f46\u662f\u7531\u4e8enew\u548cdelete\u4e0d\u80fd\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u4e5f\u4e0d\u652f\u6301\u81ea\u5b9a\u4e49\u5220\u9664\u5668&#xff0c;\u5bfc\u81f4\u4f7f\u7528\u8be5\u65b9\u5f0f\u5bb9\u6613\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u6216\u662f\u53cc\u91cd\u91ca\u653e\u7b49\u95ee\u9898\u3002\u8fd9\u5c31\u662f\u667a\u80fd\u6307\u9488&#xff08;Smart Pointers&#xff09;\u767b\u573a\u7684\u539f\u56e0\u3002<\/p>\n<p>\u5728\u5bf9\u8c61\u6784\u9020\u65f6\u83b7\u53d6\u8d44\u6e90&#xff0c;\u63a5\u7740\u63a7\u5236\u5bf9\u8d44\u6e90\u7684\u8bbf\u95ee\u4f7f\u4e4b\u5728\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5185\u59cb\u7ec8\u4fdd\u6301\u6709\u6548\u3002<\/p>\n<p>\u5728\u5bf9\u8c61\u6790\u6784\u7684\u65f6\u5019\u91ca\u653e\u8d44\u6e90\u3002\u501f\u6b64&#xff0c;\u6211\u4eec\u5b9e\u9645\u4e0a\u628a\u7ba1\u7406\u4e00\u4efd\u8d44\u6e90\u7684\u8d23\u4efb\u6258\u7ba1\u7ed9\u4e86\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>\u8fd9\u79cd\u505a\u6cd5\u6709\u4e24\u5927\u597d\u5904&#xff1a;<\/p>\n<ul>\n<li>\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<li>\u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f&#xff0c;\u5bf9\u8c61\u6240\u9700\u7684\u8d44\u6e90\u5728\u5176\u751f\u547d\u671f\u5185\u59cb\u7ec8\u4fdd\u6301\u6709\u6548<\/li>\n<\/ul>\n<p>\u00a0 &#x1f4d2;\u667a\u80fd\u6307\u9488\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u6307\u9488&#xff0c;\u5b83\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u62e5\u6709\u751f\u547d\u5468\u671f<span style=\"color:#1c7892\">&#xff08;Lifetime&#xff09;<\/span>\u3002\u5f53\u667a\u80fd\u6307\u9488\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\u65f6&#xff0c;\u5b83\u4f1a\u81ea\u52a8\u91ca\u653e\u6240\u62e5\u6709\u7684\u8d44\u6e90\u3002\u8fd9\u79cd\u81ea\u52a8\u7ba1\u7406\u673a\u5236\u6781\u5927\u5730\u51cf\u5c11\u4e86\u7a0b\u5e8f\u5458\u7684\u8d1f\u62c5&#xff0c;\u4e5f\u964d\u4f4e\u4e86\u51fa\u9519\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<p><span style=\"color:#956fe7\">\u201cThe best code is no code at all.\u201d<\/span> &#8211; Jeff Atwood<\/p>\n<p>\u8fd9\u53e5\u8bdd\u5728\u8fd9\u91cc\u975e\u5e38\u5408\u9002\u3002\u3010\u8d8a\u5c11\u7684\u4ee3\u7801\u7528\u4e8e\u7ba1\u7406\u5185\u5b58&#xff0c;\u8d8a\u5c11\u7684\u5730\u65b9\u4f1a\u51fa\u9519\u3002\u3011\u667a\u80fd\u6307\u9488\u5c31\u662f\u8fd9\u6837\u4e00\u79cd\u5de5\u5177&#xff0c;\u8ba9\u4f60\u80fd\u66f4\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91\u800c\u975e\u5185\u5b58\u7ba1\u7406\u3002<\/p>\n<h4 id=\"1.3%20%E5%83%8F%E6%8C%87%E9%92%88%E4%B8%80%E6%A0%B7%E4%BD%BF%E7%94%A8\" style=\"margin-left:0px\">1.3 \u50cf\u6307\u9488\u4e00\u6837\u4f7f\u7528<\/h4>\n<p>\u4e0b\u9762\u5c06\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u667a\u80fd\u6307\u9488SmartPtr\u4e3a\u4f8b&#xff1a;<\/p>\n<p>\u200b\/\/ \u6a21\u677f<br \/>\ntemplate&lt;class T&gt;<br \/>\nclass SmartPtr {<br \/>\npublic:<br \/>\nSmartPtr(T* ptr &#061; nullptr) \/\/\u5f53\u667a\u80fd\u6307\u9488\u672a\u521d\u59cb\u5316\u65f6&#xff0c;\u8d4b\u4e88nullptr\u7f3a\u7701\u503c<br \/>\n: _ptr(ptr)<br \/>\n{}<br \/>\n~SmartPtr(){<br \/>\nif (_ptr)<br \/>\ndelete _ptr;<br \/>\n}<br \/>\nT&amp; operator*() { return *_ptr; }<br \/>\nT* operator-&gt;() { return _ptr; }<br \/>\nprivate:<br \/>\nT* _ptr;<br \/>\n};<br \/>\nint main()<br \/>\n{<br \/>\n    SmartPtr&lt;int&gt; p(new int(1));<br \/>\n    cout &lt;&lt; *p &lt;&lt; endl;<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u5176\u4e2d\u91cd\u8f7d\u4e86*\u548c-&gt;\u8fd0\u7b97\u7b26&#xff0c;\u4f7f\u5f97\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u5c31\u50cf\u4f7f\u7528\u6307\u9488\u4e00\u6837\u3002\u667a\u80fd\u6307\u9488\u662f\u4e00\u4e2a\u6a21\u677f\u7c7b&#xff0c;\u4ee5\u80fd\u591f\u7ba1\u7406\u4efb\u4f55\u7c7b\u578b\u7684\u6307\u9488\u5f15\u7528\u7684\u5185\u5b58&#xff0c;\u5982\u679c\u6a21\u677f\u53c2\u6570\u662f\u4e00\u4e2a\u6709\u516c\u6709\u6210\u5458\u7684\u7c7b&#xff0c;\u90a3\u4e48\u8fd8\u80fd\u4f7f\u7528-&gt;\u8bbf\u95ee\u5176\u6210\u5458\u3002<\/p>\n<h4 id=\"1.4%C2%A0%E6%94%AF%E6%8C%81%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E5%AF%B9%E8%B1%A1%E6%8B%B7%E8%B4%9D\">1.4\u00a0\u652f\u6301\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u62f7\u8d1d<\/h4>\n<p>\u4e0a\u9762\u5b9e\u73b0\u7684\u667a\u80fd\u6307\u9488SmartPtr\u662f\u6781\u4e0d\u5b8c\u5584\u7684&#xff0c;\u5982\u679c\u60f3\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u548c\u62f7\u8d1d\u8d4b\u503c&#xff1a;<\/p>\n<p>int main(){<br \/>\nSmartPtr&lt;int&gt; ap1(new int(1));<br \/>\nSmartPtr&lt;int&gt; ap2(ap1); \/\/\u62f7\u8d1d\u6784\u9020<\/p>\n<p>SmartPtr&lt;int&gt; ap3(new int(2));<br \/>\n    SmartPtr&lt;int&gt; ap4(new int(2));<br \/>\n    ap4 &#061; ap3; \/\/ \u62f7\u8d1d\u8d4b\u503c<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<p>\u9519\u8bef&#xff08;Clion&#xff09;&#xff1a;<\/p>\n<p>malloc: *** error for object 0x600003c84030: pointer being freed was not allocated<\/p>\n<p>\u9020\u6210\u7a0b\u5e8f\u5d29\u6e83\u7684\u539f\u56e0&#xff1a;\u662f\u5728\u8fd9\u4e2a\u7c7b\u4e2d\u6ca1\u6709\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570&#xff0c;\u800c\u7f16\u8bd1\u5668\u9ed8\u8ba4\u751f\u6210\u7684\u5168\u90fd\u662f\u5bf9\u5185\u7f6e\u7c7b\u578b\u7684\u6d45\u62f7\u8d1d&#xff08;\u503c\u62f7\u8d1d&#xff09;&#xff1a;\u76f8\u5f53\u4e8eap1\u548cap2\u3001ap3\u548cap4\u5171\u540c\u7ba1\u7406\u540c\u4e00\u5757\u7a7a\u95f4\u3002\u5f53\u51fa\u4e86ap1\u7684\u4f5c\u7528\u57df\u540e&#xff0c;\u8c03\u7528\u6790\u6784\u51fd\u6570&#xff0c;\u91ca\u653e\u7a7a\u95f4&#xff0c;ap2\u518d\u6b21\u8c03\u7528\u6790\u6784\u51fd\u6570\u65f6\u5bfc\u81f4\u8fd9\u5757\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u7a7a\u95f4\u518d\u6b21\u88ab\u91ca\u653e\u3002ap3\u548cap4\u540c\u7406\u3002\u00a0<\/p>\n<p><span style=\"color:#fe2c24\">\u8981\u89e3\u51b3\u6d45\u62f7\u8d1d\u9020\u6210\u7684\u4e8c\u6b21\u6790\u6784\u95ee\u9898&#xff0c;\u5c31\u5fc5\u987b\u8981\u53bb\u5b9e\u73b0\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570\u5417&#xff1f;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u7b54\u6848\u662f\u4e0d\u7528\u7684&#xff0c;\u667a\u80fd\u6307\u9488\u7684\u529f\u80fd\u9700\u6c42\u662f\u6a21\u62df\u6307\u9488\u7684\u4f7f\u7528&#xff0c;\u672c\u8d28\u662f\u5e2e\u6307\u9488\u6258\u7ba1\u8d44\u6e90&#xff0c;\u90a3\u4e48\u6307\u9488\u7684\u62f7\u8d1d\u6216\u8d4b\u503c\u64cd\u4f5c\u5c31\u76f8\u5f53\u4e8e\u4e24\u4e2a\u6307\u9488\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u3002\u8d44\u6e90\u7ba1\u7406\u6743\u8f6c\u79fb&#xff0c;\u901a\u8fc7\u4e0d\u8d1f\u8d23\u4efb\u7684\u62f7\u8d1d&#xff0c;\u4f1a\u5bfc\u81f4\u88ab\u62f7\u8d1d\u5bf9\u8c61\u60ac\u7a7a\u3002\u867d\u7136\u8d44\u6e90\u80fd\u5f97\u5230\u91ca\u653e&#xff0c;\u4f46\u662f\u4f1a\u9020\u6210\u5782\u60ac\u6307\u9488\u3002\u667a\u80fd\u6307\u9488\u5c06\u5185\u5b58\u8d44\u6e90\u7684\u7ba1\u7406\u548c\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u7ed1\u5b9a\u5728\u4e00\u8d77&#xff0c;\u5982\u679c\u53ea\u662f\u50cf\u4e0a\u9762\u4e00\u6837\u7b80\u5355\u5730\u6ee1\u8db3RAII&#xff0c;\u90a3\u4e48\u4e00\u5b9a\u4f1a\u53d1\u751f\u4e8c\u6b21\u6790\u6784\u7684\u95ee\u9898&#xff0c;\u56e0\u4e3a\u521b\u5efa\u7684\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u4e00\u5b9a\u4f1a\u8c03\u7528\u6790\u6784\u51fd\u6570&#xff0c;\u4e14\u4e0d\u8bba\u7a0b\u5e8f\u662f\u5426\u6b63\u5e38\u7ed3\u675f\u3002<\/span><\/p>\n<ul>\n<li>\n<p>\u7a0b\u5e8f\u6b63\u5e38\u7ed3\u675f&#xff1a;\u5bf9\u8c61\u51fa\u4e86\u4f5c\u7528\u57df\u8c03\u7528\u6790\u6784\u51fd\u6570&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u7a0b\u5e8f\u4e0d\u6b63\u5e38\u7ed3\u675f&#xff1a;\u4f8b\u5982\u629b\u5f02\u5e38&#xff0c;\u8df3\u8f6c\u5230catch\u5757\u76f8\u5f53\u4e8e\u8df3\u8f6c\u5230\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u6808\u5e27\u4e2d&#xff0c;\u4e5f\u76f8\u5f53\u4e8e\u51fa\u4e86\u4f5c\u7528\u57df&#xff0c;\u4f9d\u7136\u8c03\u7528\u6790\u6784\u51fd\u6570\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u540e\u9762\u4ee5\u6807\u51c6\u5e93\u4e2d&#xff08;C&#043;&#043;98&#xff09;\u667a\u80fd\u6307\u9488auto_ptr\u4e3a\u4f8b\u3002<\/p>\n<\/p>\n<h3 id=\"2.%20C%2B%2B%E6%A0%87%E5%87%86%E5%BA%93%E7%9A%84%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88\">2. C&#043;&#043;\u6807\u51c6\u5e93\u7684\u667a\u80fd\u6307\u9488<\/h3>\n<p>\u5728c&#043;&#043;\u4e2d&#xff0c;\u667a\u80fd\u6307\u9488\u4e00\u5171\u5b9a\u4e49\u4e864\u79cd&#xff1a;auto_ptr\u3001unique_ptr\u3001shared_ptr \u548c weak_ptr\u3002<\/p>\n<\/p>\n<p>&#x1f4dd;\u5176\u4e2d&#xff0c;auto_ptr \u5728 C&#043;&#043;11\u5df2\u88ab\u6452\u5f03&#xff0c;\u5728C&#043;&#043;17\u4e2d\u5df2\u7ecf\u79fb\u9664\u4e0d\u53ef\u7528\u3002<\/p>\n<p>&#x1f4d9;\u867d\u7136auto_ptr\u6709\u5f88\u591a\u95ee\u9898&#xff0c;\u4f46\u662f\u4e5f\u6ca1\u6709\u53d6\u6d88&#xff0c;\u56e0\u4e3a\u53ef\u80fd\u6709\u4eba\u5728\u7528&#xff0c;\u56e0\u6b64\u540e\u9762\u5f04\u4e86\u4e00\u4e2a<\/p>\n<p>Boost\u5e93&#xff0c;\u5c5e\u4e8eC&#043;&#043;\u6269\u5c55\u5e93&#xff08;\u7b2c\u4e09\u65b9\u5e93&#xff09;&#xff0c;\u4e0d\u5c5e\u4e8eC&#043;&#043;\u6807\u51c6\u5e93&#xff08;C&#043;&#043;\u6807\u51c6\u5e93\u662f\u5199\u4e86\u5934\u6587\u4ef6\u5c31\u80fd\u7528&#xff09;<\/p>\n<p><span style=\"color:#0d0016\">\u6ce8\u610f&#xff1a;\u672c\u6587\u4e3b\u8981\u8bb2\u7684\u5c31\u662f\u540e\u9762\u7684 \u4e09\u79cd\u667a\u80fd\u6307\u9488\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"342\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042655-6839338fc478a.png\" width=\"1200\" \/><\/p>\n<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:650px\">\n<tbody>\n<tr>\u667a\u80fd\u6307\u9488\u7c7b\u578b<\/tr>\n<tr>\n<td style=\"text-align:center\">unique_ptr<\/td>\n<td style=\"text-align:center\">\u72ec\u5360\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u8be5\u6307\u9488\u72ec\u5360\u5bf9\u8c61\u7684\u6240\u6709\u6743&#xff0c;\u6bcf\u4e2a\u5bf9\u8c61\u667a\u80fd\u6709\u4e00\u4e2a\u8be5\u6307\u9488<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">shared_ptr<\/td>\n<td style=\"text-align:center\">\u5171\u4eab\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u591a\u4e2a\u5171\u4eab\u6307\u9488\u53ef\u4ee5\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">weak_ptr<\/td>\n<td style=\"text-align:center\">\u5f31\u5f15\u7528\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u8be5\u6307\u9488\u662f\u5bf9\u8c61\u7684\u4e00\u4e2a\u975e\u62e5\u6709\u6027\u5f15\u7528&#xff0c;\u4e0d\u5171\u4eab\u6307\u9488&#xff0c;\u4e0d\u80fd\u64cd\u4f5c\u8d44\u6e90&#xff0c;\u7528\u6765\u6307\u5411\u4e00\u4e2ashared_ptr&#xff0c;\u4e3b\u8981\u7528\u6765\u907f\u514dshared_ptr\u5faa\u73af\u5f15\u7528\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u9732<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e3a\u4e86\u65b9\u4fbf\u6211\u4eec\u5bf9\u4e0b\u9762\u667a\u80fd\u4e4b\u7c7b\u7684\u7406\u89e3&#xff0c;\u6211\u4eec\u5b9a\u4e49\u5982\u4e0b\u7684\u7c7b<\/p>\n<p>\/\/ Date\u7c7b<br \/>\nstruct Date{<br \/>\nint _year;<br \/>\nint _month;<br \/>\nint _day;<\/p>\n<p>Date (int year &#061; 1, int month &#061; 1, int day &#061; 1)<br \/>\n: _year(year)<br \/>\n, _month(month)<br \/>\n, _day(day)<br \/>\n{}<\/p>\n<p>~Date() {<br \/>\ncout &lt;&lt; &#034;~Date()&#034; &lt;&lt; endl;<br \/>\n}<br \/>\n};<br \/>\n\/\/\u6307\u5b9a\u5220\u9664\u5668\u6a21\u677f<br \/>\ntemplate&lt;class T&gt;<br \/>\nclass DeleteArray{<br \/>\npublic:<br \/>\nvoid operator()(T* ptr){<br \/>\ndelete[] ptr;<br \/>\n}<br \/>\n};<br \/>\n\/\/\u6587\u4ef6\u5220\u9664\u7684\u6a21\u677f<br \/>\nclass Fclose{<br \/>\npublic:<br \/>\nvoid operator()(FILE* ptr){<br \/>\ncout &lt;&lt; &#034;fclose:&#034; &lt;&lt; ptr &lt;&lt; endl;<br \/>\nfclose(ptr);<br \/>\n}<br \/>\n};<br \/>\n\/\/Test\u7c7b<br \/>\nclass Test<br \/>\n{<br \/>\npublic:<br \/>\nTest() : _num(0)<br \/>\n{<br \/>\ncout &lt;&lt; &#034;Test() \u6784\u9020\u6210\u529f&#8230;&#034; &lt;&lt; endl;<br \/>\n}<br \/>\nTest(int x) : _num(0)<br \/>\n{<br \/>\ncout &lt;&lt; &#034;Test(int x) \u6784\u9020\u6210\u529f, x &#061; &#034; &lt;&lt; x &lt;&lt; endl;<br \/>\n}<br \/>\nTest(string str) : _num(0)<br \/>\n{<br \/>\ncout &lt;&lt; &#034;Test(string str) \u6784\u9020\u6210\u529f, str &#061; &#034; &lt;&lt; str &lt;&lt; endl;<br \/>\n}<br \/>\n~Test()<br \/>\n{<br \/>\ncout &lt;&lt; &#034;~Test() \u6790\u6784\u6210\u529f&#8230;&#034; &lt;&lt; endl;<br \/>\n}<br \/>\nvoid setValue(int v)<br \/>\n{<br \/>\nthis-&gt;_num &#061; v;<br \/>\n}<br \/>\nvoid print()<br \/>\n{<br \/>\ncout &lt;&lt; &#034;_num: &#034; &lt;&lt; this-&gt;_num &lt;&lt; endl;<br \/>\n}<br \/>\nprivate:<br \/>\nint _num;<br \/>\n};<\/p>\n<hr \/>\n<h4 id=\"2.1%20auto_ptr\">2.1 auto_ptr<\/h4>\n<h5 id=\"2.1.1%20%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5\">&#x1f308;2.1.1 \u57fa\u672c\u6982\u5ff5<\/h5>\n<p>\u4e3a\u4e86\u89e3\u51b3\u4e0a\u97621.4 \u91cc\u9762\u7684\u95ee\u9898&#xff0c;\u7531\u4e8e\u51fa\u73b0\u591a\u6b21\u6790\u6784\u95ee\u9898\u7684\u672c\u8d28\u662f\u540c\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u88ab\u591a\u4e2a\u5bf9\u8c61\u901a\u8fc7\u7ba1\u7406&#xff0c;\u56e0\u6b64\u5982\u679c\u5c06\u8d44\u6e90\u7684\u7ba1\u7406\u6743\u53ea\u4ea4\u7ed9\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5c31\u4e0d\u4f1a\u51fa\u73b0\u591a\u6b21\u6790\u6784\u95ee\u9898\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"461\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042656-6839339018d13.png\" width=\"1200\" \/><\/p>\n<h5 id=\"2.1.2%C2%A0%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95\" style=\"margin-left:0px\">&#x1f308;<span style=\"color:#0d0016\"><span style=\"background-color:#ffffff\">2.1.2\u00a0\u57fa\u672c\u7528\u6cd5<\/span><\/span><\/h5>\n<p>int main(){<br \/>\nauto_ptr&lt;Date&gt; ap1(new Date);<br \/>\n\/\/\u62f7\u8d1d\u65f6&#xff0c;\u7ba1\u7406\u6743\u9650\u8f6c\u79fb&#xff0c;\u88ab\u62f7\u8d1d\u60ac\u7a7a<br \/>\nauto_ptr&lt;Date&gt; ap2(ap1);<br \/>\n\/\/\u4e0b\u9762\u8fd9\u6837\u4f1a\u4f7f\u7a0b\u5e8f\u4f1a\u5d29&#xff0c;\u56e0\u4e3aauto_ptr\u7684\u62f7\u8d1d\u662f\u7ba1\u7406\u6743\u8f6c\u79fb&#xff0c;\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4ap1\u7a7a\u4e86<br \/>\nap1-&gt;_year&#043;&#043;;<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<p>\u7136\u800c&#xff0c;\u5c06\u4e00\u4e2a\u5bf9\u8c61\u5bf9\u8d44\u6e90\u7684\u7ba1\u7406\u6743\u8f6c\u79fb\u540e&#xff0c;\u5c31\u610f\u5473\u7740\u8fd9\u4e2a\u5bf9\u8c61\u518d\u5bf9\u8d44\u6e90\u8bbf\u95ee\u662f\u4e00\u4e2a\u975e\u6cd5\u64cd\u4f5c&#xff0c;\u7a0b\u5e8f\u4f1a\u56e0\u6b64\u5d29\u6e83\u3002\u5982\u679c\u8ba9\u4e0d\u719f\u6089auto_ptr\u539f\u7406\u7684\u4eba\u4f7f\u7528&#xff0c;\u56e0\u4e3a\u62f7\u8d1d\u64cd\u4f5c\u800c\u9020\u6210\u975e\u6cd5\u6307\u9488\u6216\u5185\u5b58\u6cc4\u6f0f\u662f\u6709\u53ef\u80fd\u7684&#xff08;\u62f7\u8d1d\u60ac\u7a7a&#xff09;&#xff0c;\u800c\u8fd9\u4e5f\u662f\u81f4\u547d\u7684\u9519\u8bef&#xff0c;\u56e0\u6b64\u8bb8\u591a\u516c\u53f8\u660e\u6587\u89c4\u5b9a\u7981\u6b62auto_ptr\u7684\u4f7f\u7528&#xff0c;\u8fdb\u800c\u7531C&#043;&#043;11\u7684unique_ptr\u548cshared_ptr\u53d6\u4ee3\u3002<\/p>\n<hr \/>\n<h4 id=\"2.2%C2%A0unique_ptr\">2.2\u00a0unique_ptr<\/h4>\n<h5 id=\"2.2.1%20%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5\">\u27282.2.1 \u57fa\u672c\u6982\u5ff5<\/h5>\n<p>\u51fa\u73b0\u591a\u6b21\u6790\u6784\u95ee\u9898\u7684\u672c\u8d28\u662f\u540c\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u88ab\u591a\u4e2a\u5bf9\u8c61\u901a\u8fc7\u7ba1\u7406&#xff0c;\u5982\u679c\u5c06\u8d44\u6e90\u7684\u7ba1\u7406\u6743\u53ea\u4ea4\u7ed9\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5c31\u4e0d\u4f1a\u51fa\u73b0\u591a\u6b21\u6790\u6784\u95ee\u9898\u3002<\/p>\n<p>C&#043;&#043;98\u7684auto_ptr\u56e0\u4e3a\u62f7\u8d1d\u548c\u8d4b\u503c\u64cd\u4f5c\u800c\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u548c\u60ac\u5782\u6307\u9488\u7684\u95ee\u9898\u800c\u9971\u53d7\u8bdf\u75c5&#xff0c;C&#043;&#043;11\u5f15\u5165\u7684unique_ptr\u5219\u7c97\u66b4\u5730\u780d\u6389\u4e86\u5b83\u7684\u62f7\u8d1d\u548c\u8d4b\u503c\u529f\u80fd\u3002\u8fd9\u901a\u8fc7C&#043;&#043;11\u5f15\u5165\u7684\u5173\u952e\u5b57<span style=\"color:#fe2c24\">delete<\/span>\u7684\u65b0\u529f\u80fd\u5b9e\u73b0\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u5728C&#043;&#043;11\u4e4b\u524d&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u5c06\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570\u79c1\u6709\u58f0\u660e\u5b9e\u73b0\u3002<\/span><\/p>\n<\/p>\n<p>&#x1f31e;\u5b83\u5b9e\u73b0\u5bf9\u5bf9\u8c61\u7684\u72ec\u5360\u6240\u6709\u6743\u8bed\u4e49\u3002\u8fd9\u610f\u5473\u7740\u4e00\u4e2aunique_ptr\u5728\u4efb\u4f55\u65f6\u5019\u90fd\u6307\u5411\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u800c\u4e14\u8fd9\u4e2a\u5bf9\u8c61\u53ea\u80fd\u7531\u4e00\u4e2aunique_ptr\u62e5\u6709\u3002\u5f53unique_ptr\u88ab\u9500\u6bc1&#xff08;\u4f8b\u5982\u79bb\u5f00\u5176\u4f5c\u7528\u57df&#xff09;\u65f6&#xff0c;\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\u4e5f\u4f1a\u88ab\u81ea\u52a8\u5220\u9664\u3002<\/p>\n<h5 id=\"2.2.1%20%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95\">\u27282.2.2\u00a0\u57fa\u672c\u7528\u6cd5<\/h5>\n<p><span style=\"color:#4da8ee\">\u6837\u4f8b1&#xff1a;&#xff08;\u521d\u6982&#xff09;<\/span><\/p>\n<p>int main() {<br \/>\nunique_ptr&lt;Date&gt; up1(new Date);<br \/>\n\/\/\u4e0d\u652f\u6301\u62f7\u8d1d<br \/>\n\/\/unique_ptr&lt;Date&gt; up2(up1);<\/p>\n<p>\/\/unique_ptr&lt;Date&gt; up2(new Date[6]); \/\/error&#xff0c;\u56e0\u4e3a\u7c7b\u578b\u4e0d\u5339\u914d&#xff0c;\u7531\u4e8e\u6211\u4eec\u662f\u7528new []\u6784\u9020\u7684&#xff0c;\u90a3\u4e48\u5c31\u8981\u7528delete[]\u53bb\u9500\u6bc1<br \/>\n\/\/\u5b9a\u5236\u5220\u9664\u5668\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898<br \/>\nunique_ptr&lt;Date, DeleteArray&lt;Date&gt;&gt; up2(new Date[6]); \/\/\u7528\u5b9a\u5236\u7684\u5220\u9664\u5668<br \/>\n\/\/unique_ptr&lt;Date[]&gt; up2(new Date[5]);<br \/>\nunique_ptr&lt;FILE, Fclose&gt; up3(fopen(&#034;test.cpp&#034;, &#034;r&#034;));<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<h4><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"238\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042656-683933907972c.png\" width=\"934\" \/><\/p>\n<p><span style=\"color:#4da8ee\">\u6837\u4f8b2&#xff1a;&#xff08;\u8be6\u7ec6&#xff09;<\/span><\/p>\n<p>1. \u521d\u59cb\u5316<\/p>\n<ul>\n<li>std::unique_ptr\u662f\u4e00\u4e2a\u72ec\u5360\u578b\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u5b83\u4e0d\u5141\u8bb8\u5176\u4ed6\u7684\u667a\u80fd\u6307\u9488\u5171\u4eab\u5176\u5185\u90e8\u7684\u6307\u9488&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u5b83\u7684\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u4e00\u4e2a\u72ec\u5360\u667a\u80fd\u6307\u9488\u5bf9\u8c61&#xff0c;\u4f46\u662f\u4e0d\u5141\u8bb8\u901a\u8fc7\u8d4b\u503c\u5c06\u4e00\u4e2aunique_ptr\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2aunique_ptr\u3002<\/li>\n<\/ul>\n<p>\u00a02. \u6307\u5b9a\u5220\u9664\u5668<\/p>\n<ul>\n<li>\u00a0unique_ptr\u6307\u5b9a\u5220\u9664\u5668\u548cshared_ptr\u6307\u5b9a\u5220\u9664\u5668\u662f\u6709\u533a\u522b\u7684&#xff0c;unique_ptr\u6307\u5b9a\u5220\u9664\u5668\u7684\u65f6\u5019\u9700\u8981\u786e\u5b9a\u5220\u9664\u5668\u7684\u7c7b\u578b&#xff0c;\u6240\u4ee5\u4e0d\u80fd\u50cfshared_ptr\u90a3\u6837\u76f4\u63a5\u6307\u5b9a\u5220\u9664\u5668<\/li>\n<\/ul>\n<p>int main()<br \/>\n{<br \/>\n\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;  \u4e00&#xff0c;\u521d\u59cb\u5316\u667a\u80fd\u6307\u9488unique_ptr  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/<br \/>\n\/\/ 1.\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316<br \/>\nunique_ptr&lt;int&gt; up1(new int(3));<\/p>\n<p>\/\/unique_ptr&lt;int&gt; up &#061; up1;  \/\/ \u7f16\u8bd1\u9519\u8bef&#xff0c;\u4e0d\u80fd\u590d\u5236<\/p>\n<p>\/\/ 2.\u901a\u8fc7\u79fb\u52a8\u51fd\u6570\u521d\u59cb\u5316<br \/>\nunique_ptr&lt;int&gt; up2 &#061; move(up1); \/\/ \u73b0\u5728up2\u72ec\u5360\u8d44\u6e90&#xff0c;up1\u53d8\u4e3a\u7a7a<\/p>\n<p>\/\/ 3.\u901a\u8fc7reset,\u91ca\u653e\u8d44\u6e90\u5e76\u5c06\u6307\u9488\u7f6e\u4e3a\u7a7a&#xff0c;\u7136\u540e\u518d\u521d\u59cb\u5316<br \/>\nup1.reset(new int(7));<\/p>\n<p>\/\/\u5bf9\u6bd4reset&#xff0c;release\u4f1a\u91ca\u653e\u8d44\u6e90\u7684\u6240\u6709\u6743\u4f46\u4e0d\u5220\u9664&#xff0c;\u8fd4\u56de\u539f\u59cb\u6307\u9488<br \/>\nup1.release();<\/p>\n<p>\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;  \u4e8c&#xff0c;unique_ptr\u7684\u4f7f\u7528  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/<br \/>\n\/\/1.\u65b9\u6cd5\u4e00<br \/>\nunique_ptr&lt;Test&gt; up3(new Test(666));<br \/>\nTest* pt &#061; up3.get();<br \/>\npt-&gt;setValue(6);<br \/>\npt-&gt;print();<\/p>\n<p>\/\/2.\u65b9\u6cd5\u4e8c<br \/>\nup3-&gt;setValue(777);<br \/>\nup3-&gt;print();<\/p>\n<p>\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;  \u4e09&#xff0c;\u6307\u5b9a\u5220\u9664\u5668  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;*\/<br \/>\n1.\u51fd\u6570\u6307\u9488\u7c7b\u578b<br \/>\n\/\/using ptrFunc &#061; void(*)(Test*);<br \/>\n\/\/unique_ptr&lt;Test, ptrFunc&gt; up4(new Test(&#034;hello&#034;), [](Test* t) {<br \/>\n\/\/    cout &lt;&lt; &#034;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#034; &lt;&lt; endl;<br \/>\n\/\/    delete t;<br \/>\n\/\/    });<\/p>\n<p>\/\/2.\u4eff\u51fd\u6570\u7c7b\u578b&#xff08;\u5229\u7528\u53ef\u8c03\u7528\u5bf9\u8c61\u5305\u88c5\u5668&#xff09;<br \/>\nunique_ptr&lt;Test, function&lt;void(Test*)&gt;&gt; up4(new Test(&#034;hello&#034;), [](Test* t) {<br \/>\ncout &lt;&lt; &#034;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#034; &lt;&lt; endl;<br \/>\ndelete t;<br \/>\n});<\/p>\n<p>\/*&#8212;&#8212;&#8212;- \u56db&#xff0c;\u72ec\u5360(\u5171\u4eab)\u7684\u667a\u80fd\u6307\u9488\u53ef\u4ee5\u7ba1\u7406\u6570\u7ec4\u7c7b\u578b\u7684\u5730\u5740&#xff0c;\u80fd\u591f\u81ea\u52a8\u91ca\u653e &#8212;&#8212;&#8212;*\/<br \/>\nunique_ptr&lt;Test[]&gt; up5(new Test[3]);<\/p>\n<p>\/\/\u5728c&#043;&#043;11\u4e2dshared_ptr\u4e0d\u652f\u6301\u4e0b\u9762\u7684\u5199\u6cd5&#xff0c;c&#043;&#043;11\u4ee5\u540e\u624d\u652f\u6301\u7684<br \/>\nshared_ptr&lt;Test[]&gt; up6(new Test[3]);<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"558\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042656-683933909479d.png\" width=\"1017\" \/><\/p>\n<hr \/>\n<h4 id=\"2.3%C2%A0shared_ptr\">2.3\u00a0shared_ptr<\/h4>\n<h5 id=\"2.3.1%20%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5%E5%8F%8A%E5%8E%9F%E7%90%86\">&#x1f388;2.3.1 \u57fa\u672c\u6982\u5ff5<\/h5>\n<p>&#x1f319;shared_ptr\u662fC&#043;&#043;11\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u901a\u8fc7\u5f15\u7528\u8ba1\u6570\u7684\u65b9\u5f0f\u89e3\u51b3\u667a\u80fd\u6307\u9488\u7684\u62f7\u8d1d\u95ee\u9898\u3002<\/p>\n<p>&#x1f319;\u6bcf\u4e2a\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u6709\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u5f15\u7528\u8ba1\u6570&#xff0c;\u8fd9\u4e2a\u5f15\u7528\u8ba1\u6570\u8bb0\u5f55\u5f53\u524d\u6709\u591a\u5c11\u5bf9\u8c61\u5728\u7ba1\u7406\u8fd9\u5757\u8d44\u6e90\u3002<\/p>\n<p>&#x1f319;\u6bcf\u65b0\u589e\u52a0\u4e00\u4e2a\u5bf9\u8c61\u7ba1\u7406\u8fd9\u5757\u8d44\u6e90\u5219\u5bf9\u8be5\u8d44\u6e90\u7684\u5f15\u7528\u8ba1\u6570&#043;&#043;&#xff0c;\u5f53\u4e00\u4e2a\u5bf9\u8c61\u4e0d\u5728\u7ba1\u7406\u8fd9\u5757\u8d44\u6e90\u6216\u5bf9\u8c61\u6790\u6784\u65f6\u90a3\u4e48\u8be5\u8d44\u6e90\u5bf9\u5e94\u7684\u5f15\u7528\u8ba1\u6570 \u2013 \u2013&#xff0c;\u5f53\u4e00\u4e2a\u8d44\u6e90\u7684\u5f15\u7528\u8ba1\u6570\u4e3a0\u65f6\u90a3\u4e48\u5c31\u8bf4\u660e\u5df2\u7ecf\u6ca1\u6709\u5bf9\u8c61\u5728\u7ba1\u7406\u8fd9\u5757\u8d44\u6e90\u4e86&#xff0c;\u8fd9\u65f6\u5019\u5c31\u53ef\u4ee5\u8fdb\u884c\u91ca\u653e\u4e86\u3002<\/p>\n<p>\u5f15\u7528\u8ba1\u6570\u7684\u65b9\u5f0f\u80fd\u591f\u652f\u6301\u591a\u4e2a\u5bf9\u8c61\u4e00\u8d77\u7ba1\u7406\u4e00\u4e2a\u8d44\u6e90&#xff0c;\u4e5f\u5c31\u652f\u6301\u667a\u80fd\u6307\u9488\u7684\u62f7\u8d1d&#xff0c;\u53ea\u6709\u5f53\u8d44\u6e90\u7684\u5f15\u7528\u8ba1\u6570\u51cf\u4e3a0\u65f6\u624d\u4f1a\u91ca\u653e&#xff0c;\u4fdd\u8bc1\u4e86\u540c\u4e00\u4e2a\u8d44\u6e90\u4e0d\u4f1a\u88ab\u591a\u6b21\u91ca\u653e&#xff1a;<\/p>\n<p>&#x1f319;\u5b9e\u73b0\u4e86\u5bf9\u8c61\u7684\u5171\u4eab\u6240\u6709\u6743\u8bed\u4e49\u3002\u591a\u4e2ashared_ptr\u53ef\u4ee5\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5e76\u4e14\u6bcf\u4e2ashared_ptr\u6301\u6709\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570\u3002\u5f53\u6700\u540e\u4e00\u4e2a\u6307\u5411\u67d0\u4e2a\u5bf9\u8c61\u7684shared_ptr\u88ab\u9500\u6bc1\u6216\u91cd\u7f6e\u65f6&#xff0c;\u8be5\u5bf9<\/p>\n<p>\u8c61\u624d\u4f1a\u88ab\u5220\u9664\u3002<\/p>\n<h5 id=\"2.3.2%20%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95\">&#x1f388;2.3.2 \u57fa\u672c\u7528\u6cd5<\/h5>\n<p><span style=\"color:#4da8ee\">\u6837\u4f8b1&#xff1a;&#xff08;\u521d\u6982&#xff09;<\/span><\/p>\n<p>int main() {<br \/>\n\/\/vector&lt;shared_ptr&lt;Date&gt;&gt; v;<br \/>\nshared_ptr&lt;Date&gt; sp1(new Date);<br \/>\nshared_ptr&lt;Date&gt; sp2(sp1);<br \/>\nshared_ptr&lt;Date&gt; sp3(sp2);<\/p>\n<p>cout &lt;&lt; sp1.use_count() &lt;&lt; endl; \/\/\u67e5\u770b\u5176\u5f15\u7528\u8ba1\u6570<\/p>\n<p>\/\/\u5b9a\u5236\u5220\u9664\u5668<br \/>\nshared_ptr &lt;Date[]&gt; sp4(new Date[5]);<br \/>\n\/\/shared_ptr&lt;FILE, Fclose&gt; up5(fopen(&#034;test.cpp&#034;, &#034;r&#034;)); \/\/\u4e0d\u652f\u6301\u4f20\u8fd9\u4e2a\u6a21\u677f\u53c2\u6570<br \/>\nshared_ptr&lt;FILE&gt; up5(fopen(&#034;test.cpp&#034;, &#034;r&#034;), Fclose());<\/p>\n<p>shared_ptr&lt;Date&gt; sp6 &#061; make_shared&lt;Date&gt;(2024, 8, 5);<br \/>\nshared_ptr&lt;int&gt; sp7((int*)malloc(40), [](int* ptr)<br \/>\n{<br \/>\ncout &lt;&lt; &#034;free&#xff1a;&#034; &lt;&lt; ptr &lt;&lt; endl;<br \/>\nfree(ptr);<br \/>\n});<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"192\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042656-68393390c261f.png\" width=\"378\" \/><\/p>\n<p>1. \u521d\u59cb\u5316<\/p>\n<ul>\n<li>\u5171\u4eab\u667a\u80fd\u6307\u9488\u662f\u6307\u591a\u4e2a\u667a\u80fd\u6307\u9488\u53ef\u4ee5\u540c\u65f6\u7ba1\u7406\u540c\u4e00\u5757\u6709\u6548\u7684\u5185\u5b58&#xff0c;\u5171\u4eab\u667a\u80fd\u6307\u9488shared_ptr \u662f\u4e00\u4e2a\u6a21\u677f\u7c7b&#xff0c;\u5982\u679c\u8981\u8fdb\u884c\u521d\u59cb\u5316\u6709\u4e09\u79cd\u65b9\u5f0f&#xff1a;\u901a\u8fc7\u6784\u9020\u51fd\u6570\u3001std::make_shared\u8f85\u52a9\u51fd\u6570\u4ee5\u53careset\u65b9\u6cd5\u3002\u5171\u4eab\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u521d\u59cb\u5316\u5b8c\u6bd5\u4e4b\u540e\u5c31\u6307\u5411\u4e86\u8981\u7ba1\u7406\u7684\u90a3\u5757\u5806\u5185\u5b58&#xff0c;\u5982\u679c\u60f3\u8981\u67e5\u770b\u5f53\u524d\u6709\u591a\u5c11\u4e2a\u667a\u80fd\u6307\u9488\u540c\u65f6\u7ba1\u7406\u7740\u8fd9\u5757\u5185\u5b58\u53ef\u4ee5\u4f7f\u7528\u5171\u4eab\u667a\u80fd\u6307\u9488\u63d0\u4f9b\u7684\u4e00\u4e2a\u6210\u5458\u51fd\u6570use_count<\/li>\n<\/ul>\n<p>shared_ptr\u901a\u8fc7\u4e00\u4e2a\u6307\u9488\u4fdd\u6301\u5bf9\u4e00\u4e2a\u5bf9\u8c61\u7684\u5171\u4eab\u6240\u6709\u6743\u3002\u591a\u4e2ashared_ptr\u5bf9\u8c61\u53ef\u4ee5\u62e5\u6709\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u5f53\u4ee5\u4e0b\u60c5\u51b5\u4e4b\u4e00\u53d1\u751f\u65f6&#xff0c;\u5bf9\u8c61\u88ab\u9500\u6bc1\u5e76\u91ca\u653e\u5176\u5185\u5b58&#xff1a;<\/p>\n<ul>\n<li>\u62e5\u6709\u8be5\u5bf9\u8c61\u7684\u6700\u540e\u4e00\u4e2ashared_ptr\u88ab\u9500\u6bc1&#xff1b;<\/li>\n<li>\u901a\u8fc7reset()\u51fd\u6570\u5c06shared_ptr\u8d4b\u503c\u4e3a\u53e6\u4e00\u4e2a\u6307\u9488\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">2.\u00a0\u83b7\u53d6\u539f\u59cb\u6307\u9488<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">\u5bf9\u5e94\u57fa\u7840\u6570\u636e\u7c7b\u578b\u6765\u8bf4&#xff0c;\u901a\u8fc7\u64cd\u4f5c\u667a\u80fd\u6307\u9488\u548c\u64cd\u4f5c\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5185\u5b58\u6548\u679c\u662f\u4e00\u6837\u7684&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u5b8c\u6210\u6570\u636e\u7684\u8bfb\u5199\u3002\u4f46\u662f\u5982\u679c\u5171\u4eab\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u90a3\u4e48\u5c31\u9700\u8981\u53d6\u51fa\u539f\u59cb\u5185\u5b58\u7684\u5730\u5740\u518d\u64cd\u4f5c&#xff0c;\u53ef\u4ee5\u8c03\u7528\u5171\u4eab\u667a\u80fd\u6307\u9488\u7c7b\u63d0\u4f9b\u7684get()\u65b9\u6cd5\u5f97\u5230\u539f\u59cb\u5730\u5740<\/span><\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p>\u00a03. \u6307\u5b9a\u5220\u9664\u5668<\/p>\n<ul>\n<li>\u00a0\u00a0\u5f53\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5185\u5b58\u5bf9\u5e94\u7684\u5f15\u7528\u8ba1\u6570\u53d8\u4e3a0\u7684\u65f6\u5019&#xff0c;\u8fd9\u5757\u5185\u5b58\u5c31\u4f1a\u88ab\u667a\u80fd\u6307\u9488\u6790\u6784\u6389\u4e86\u3002\u53e6\u5916&#xff0c;\u6211\u4eec\u5728\u521d\u59cb\u5316\u667a\u80fd\u6307\u9488\u7684\u65f6\u5019\u4e5f\u53ef\u4ee5\u81ea\u5df1\u6307\u5b9a\u5220\u9664\u52a8\u4f5c&#xff0c;\u8fd9\u4e2a\u5220\u9664\u64cd\u4f5c\u5bf9\u5e94\u7684\u51fd\u6570\u88ab\u79f0\u4e4b\u4e3a\u5220\u9664\u5668&#xff0c;\u8fd9\u4e2a\u5220\u9664\u5668\u51fd\u6570\u672c\u8d28\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570&#xff0c;\u6211\u4eec\u53ea\u9700\u8981\u8fdb\u884c\u5b9e\u73b0&#xff0c;\u5176\u8c03\u7528\u662f\u7531\u667a\u80fd\u6307\u9488\u5b8c\u6210\u7684\u3002<\/li>\n<\/ul>\n<p>4. \u5f15\u7528\u8ba1\u6570<\/p>\n<ul>\n<li>auto_ptr\u8f6c\u79fb\u8d44\u6e90\u540e\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u548c\u60ac\u5782\u6307\u9488\u7684\u4e3b\u8981\u539f\u56e0\u5c31\u662f\u6bcf\u4e2aauto_ptr\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u7ba1\u7406\u7684\u8d44\u6e90\u662f\u5404\u81ea\u72ec\u7acb\u7684&#xff0c;\u975e\u6b64\u5373\u5f7c\u3002shared_ptr\u5171\u4eab\u540c\u4e00\u4e2a\u8d44\u6e90&#xff0c;\u5185\u5b58\u8d44\u6e90\u53ea\u5728\u6700\u540e\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u89e3\u9664\u5f15\u7528\u65f6\u91ca\u653e&#xff0c;\u8fd9\u6837\u5c31\u4e0d\u4f1a\u9020\u6210\u8d44\u6e90\u88ab\u5355\u65b9\u9762\u5730\u63a5\u7ba1\u9020\u6210\u7684\u95ee\u9898\u3002<\/li>\n<li>\u5f15\u7528\u8ba1\u6570\u4f7f\u5f97\u4e00\u4e2a\u7a7a\u95f4\u53ef\u4ee5\u88ab\u591a\u4e2a\u5bf9\u8c61\u7ba1\u7406&#xff0c;\u5f53\u5f15\u7528\u8ba1\u6570\u4e3a0\u65f6&#xff0c;\u8bf4\u660e\u5df2\u7ecf\u6ca1\u6709\u667a\u80fd\u6307\u9488\u7ba1\u7406\u8fd9\u5757\u5185\u5b58\u7a7a\u95f4\u4e86&#xff0c;\u6b64\u65f6\u624d\u80fd\u91ca\u653e\u8d44\u6e90&#xff0c;\u5f25\u8865\u4e86auto_ptr\u7684\u7f3a\u9677\u3002\u8981\u77e5\u9053\u5f15\u7528\u8ba1\u6570\u7684\u503c&#xff0c;\u53ea\u9700\u8981\u8c03\u7528shared_ptr\u7684\u6210\u5458\u51fd\u6570use_count()\u5373\u53ef\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"730\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042656-68393390d74a5.png\" width=\"1200\" \/><\/p>\n<p><span style=\"color:#4da8ee\">\u6837\u4f8b2&#xff1a;&#xff08;\u8be6\u7ec6&#xff09;<\/span><\/p>\n<p>int main()<br \/>\n{<br \/>\n\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-  \u4e00&#xff0c;\u521d\u59cb\u5316\u667a\u80fd\u6307\u9488shared_ptr  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/<br \/>\n\/\/ 1.\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316<br \/>\nshared_ptr&lt;int&gt; sp1(new int(3));<br \/>\ncout &lt;&lt; &#034;sp1\u7ba1\u7406\u7684\u5185\u5b58\u5f15\u7528\u8ba1\u6570&#xff1a;&#034; &lt;&lt; sp1.use_count() &lt;&lt; endl;<\/p>\n<p>\/\/ 2.\u901a\u8fc7\u79fb\u52a8\u548c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316<br \/>\nshared_ptr&lt;int&gt; sp2 &#061; move(sp1);<br \/>\ncout &lt;&lt; &#034;sp1\u7ba1\u7406\u7684\u5185\u5b58\u5f15\u7528\u8ba1\u6570&#xff1a;&#034; &lt;&lt; sp1.use_count() &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;sp2\u7ba1\u7406\u7684\u5185\u5b58\u5f15\u7528\u8ba1\u6570&#xff1a;&#034; &lt;&lt; sp2.use_count() &lt;&lt; endl;<\/p>\n<p>shared_ptr&lt;int&gt; sp3 &#061; sp2; \/\/\u8d4b\u503c<br \/>\ncout &lt;&lt; &#034;sp2\u7ba1\u7406\u7684\u5185\u5b58\u5f15\u7528\u8ba1\u6570: &#034; &lt;&lt; sp2.use_count() &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;sp3\u7ba1\u7406\u7684\u5185\u5b58\u5f15\u7528\u8ba1\u6570: &#034; &lt;&lt; sp3.use_count() &lt;&lt; endl;<\/p>\n<p>\/\/ 3.\u901a\u8fc7 std::make_shared\u521d\u59cb\u5316<br \/>\nshared_ptr&lt;int&gt; sp4 &#061; make_shared&lt;int&gt;(8);<br \/>\nshared_ptr&lt;Test&gt; sp5 &#061; make_shared&lt;Test&gt;(7);<br \/>\nshared_ptr&lt;Test&gt; sp6 &#061; make_shared&lt;Test&gt;(&#034;Love Life&#034;);<\/p>\n<p>\/\/ 4.\u901a\u8fc7reset\u521d\u59cb\u5316<br \/>\nsp6.reset();\/\/\u91cd\u7f6esp6, ps6\u7684\u5f15\u7528\u57fa\u6570\u4e3a0<br \/>\ncout &lt;&lt; &#034;sp6\u7ba1\u7406\u7684\u5185\u5b58\u5f15\u7528\u8ba1\u6570: &#034; &lt;&lt; sp6.use_count() &lt;&lt; endl;<\/p>\n<p>sp5.reset(new Test(&#034;hello&#034;)); \/\/\u91cd\u7f6e\u4e86\u6307\u9488\u7684\u6307\u5411\u5bf9\u8c61&#xff0c;\u539f\u6765\u7684\u5bf9\u8c61\u5df2\u7ecf\u91ca\u653e<br \/>\ncout &lt;&lt; &#034;sp5\u7ba1\u7406\u7684\u5185\u5b58\u5f15\u7528\u8ba1\u6570: &#034; &lt;&lt; sp5.use_count() &lt;&lt; endl;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; endl;<\/p>\n<p>\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;  \u4e8c&#xff0c;\u5171\u4eab\u667a\u80fd\u6307\u9488shared_ptr\u7684\u4f7f\u7528  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/<br \/>\n\/\/ 1.\u65b9\u6cd5\u4e00<br \/>\nTest* t &#061; sp5.get();<br \/>\nt-&gt;setValue(1000);<br \/>\nt-&gt;print();<\/p>\n<p>\/\/ 2.\u65b9\u6cd5\u4e8c<br \/>\nsp5-&gt;setValue(7777);<br \/>\nsp5-&gt;print();<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; endl;<\/p>\n<p>\/\/\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;  \u4e09&#xff0c;\u6307\u5b9a\u5220\u9664\u5668  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;*\/<br \/>\n \/\/ 1.\u7b80\u5355\u4e3e\u4f8b<br \/>\nshared_ptr&lt;Test&gt; ppp(new Test(100), [](Test* t) {<br \/>\n\/\/\u91ca\u653e\u5185\u5b58<br \/>\ncout &lt;&lt; &#034;Test\u5bf9\u8c61\u7684\u5185\u5b58\u88ab\u91ca\u653e\u4e86&#8230;&#8230;.&#034; &lt;&lt; endl;<br \/>\ndelete t;<br \/>\n});<br \/>\nprintf(&#034;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-\\\\n&#034;);<\/p>\n<p> 2.\u5982\u679c\u662f\u6570\u7ec4\u7c7b\u578b\u7684\u5730\u5740&#xff0c;\u5c31\u9700\u8981\u81ea\u5df1\u5199\u6307\u5b9a\u5220\u9664\u5668&#xff0c;\u5426\u5219\u5185\u5b58\u65e0\u6cd5\u5168\u90e8\u91ca\u653e<br \/>\n\/\/\/\/shared_ptr&lt;Test&gt; p1(new Test[5], [](Test* t) {<br \/>\n\/\/\/\/    delete[]t;<br \/>\n\/\/\/\/    });<\/p>\n<p>3.\u4e5f\u53ef\u4ee5\u4f7f\u7528c&#043;&#043;\u7ed9\u6211\u4eec\u63d0\u4f9b\u7684 \u9ed8\u8ba4\u5220\u9664\u5668\u51fd\u6570&#xff08;\u51fd\u6570\u6a21\u677f&#xff09;<br \/>\nshared_ptr&lt;Test&gt; p2(new Test[3], default_delete&lt;Test[]&gt;());<\/p>\n<p>4.c&#043;&#043;11\u4ee5\u540e\u53ef\u4ee5\u8fd9\u6837\u5199 \u4e5f\u53ef\u4ee5\u81ea\u52a8\u91ca\u653e\u5185\u5b58<br \/>\nshared_ptr&lt;Test[]&gt; p3(new Test[3]);<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"797\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042657-6839339159f5e.png\" width=\"1200\" \/><\/p>\n<p>\u00a0\u6b64\u5916\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c01\u88c5\u4e00\u4e2a\u51fd\u6570\u6a21\u677fmake_shared_array\u65b9\u6cd5\u6765\u8ba9shared_ptr\u652f\u6301\u6570\u7ec4&#xff1a;<\/p>\n<p>template &lt;typename T&gt;<br \/>\nshared_ptr&lt;T&gt; make_share_array(size_t size)<br \/>\n{<br \/>\n\/\/\u8fd4\u56de\u533f\u540d\u5bf9\u8c61<br \/>\nreturn shared_ptr&lt;T&gt;(new T[size], default_delete&lt;T[]&gt;());<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\nshared_ptr&lt;int&gt; ptr1 &#061; make_share_array&lt;int&gt;(10);<br \/>\ncout &lt;&lt; ptr1.use_count() &lt;&lt; endl;<\/p>\n<p>shared_ptr&lt;string&gt; ptr2 &#061; make_share_array&lt;string&gt;(7);<br \/>\ncout &lt;&lt; ptr2.use_count() &lt;&lt; endl;<br \/>\n} <\/p>\n<h5 id=\"2.3.3%20%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\">&#x1f388;2.3.3 \u6ce8\u610f\u4e8b\u9879<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0shared_ptr\u4e2d\u7684\u5f15\u7528\u8ba1\u6570\u662f\u5b58\u653e\u5728\u5806\u533a&#xff0c;\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u8ba9\u6240\u6709\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684shared_ptr\u3002\u5982\u679c\u5f15\u7528\u8ba1\u6570\u5728\u6808\u533a&#xff0c;\u90a3\u4e48\u5f53\u4e00\u4e2ashared_ptr\u6539\u53d8\u6307\u5411\u6216\u8005\u79bb\u5f00\u4f5c\u7528\u57df\u65f6&#xff0c;\u5c31\u65e0\u6cd5\u901a\u77e5\u5176\u4ed6shared_ptr\u66f4\u65b0\u5f15\u7528\u8ba1\u6570\u4e86\u3002\u56e0\u6b64&#xff0c;\u5f15\u7528\u8ba1\u6570\u4e5f\u4e0d\u80fd\u662f\u9759\u6001\u6210\u5458&#xff0c;\u6bcf\u4e2a\u7c7b\u578b\u5b9e\u4f8b\u5316\u7684\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u65f6\u5171\u7528\u9759\u6001\u6210\u5458&#xff0c;\u8fd9\u4f1a\u5bfc\u81f4\u7ba1\u7406\u76f8\u540c\u8d44\u6e90\u7684\u5bf9\u8c61\u548c\u7ba1\u7406\u4e0d\u540c\u8d44\u6e90\u7684\u5bf9\u8c61\u5171\u7528\u540c\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570\u3002<\/p>\n<p>\u7531\u4e8e\u5728\u5806\u533a\u7684\u5f15\u7528\u8ba1\u6570\u548c\u540c\u4e00\u7c7b\u578b\u7684\u667a\u80fd\u6307\u9488\u662f\u7ed1\u5b9a\u5728\u4e00\u8d77\u7684&#xff0c;\u5f53\u667a\u80fd\u6307\u9488\u91ca\u653e\u8d44\u6e90\u65f6&#xff0c;\u4e5f\u9700\u8981\u91ca\u653e\u5f15\u7528\u8ba1\u6570\u5360\u7528\u7684\u5185\u5b58\u3002<\/p>\n<hr \/>\n<h4 id=\"2.4%C2%A0weak_ptr\">2.4\u00a0weak_ptr<\/h4>\n<h5 id=\"2.4.1%20%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5\">&#x1f9e9;2.4.1 \u57fa\u672c\u6982\u5ff5<\/h5>\n<p>\u2b50\u5f31\u5f15\u7528\u667a\u80fd\u6307\u9488std::weak_ptr\u53ef\u4ee5\u770b\u505a\u662fshared_ptr\u7684\u52a9\u624b&#xff0c;\u662f\u5bf9shared_ptr\u6240\u7ba1\u7406\u5bf9\u8c61\u7684\u4e00\u4e2a\u975e\u62e5\u6709\u6027\u5f15\u7528&#xff0c;\u5b83\u4e0d\u7ba1\u7406shared_ptr\u5185\u90e8\u7684\u6307\u9488\u3002std::weak_ptr\u6ca1\u6709\u91cd\u8f7d\u64cd\u4f5c\u7b26*\u548c-&gt;&#xff0c;\u56e0\u4e3a\u5b83\u4e0d\u5171\u4eab\u6307\u9488&#xff0c;\u4e0d\u80fd\u64cd\u4f5c\u8d44\u6e90&#xff0c;\u6240\u4ee5\u5b83\u7684\u6784\u9020\u4e0d\u4f1a\u589e\u52a0\u5f15\u7528\u8ba1\u6570&#xff0c;\u6790\u6784\u4e5f\u4e0d\u4f1a\u51cf\u5c11\u5f15\u7528\u8ba1\u6570\u3002\u6700\u540e\u4e00\u4e2a\u6307\u5411\u5bf9\u8c61\u7684shared_ptr\u88ab\u9500\u6bc1\u65f6&#xff0c;\u65e0\u8bba\u662f\u5426\u8fd8\u6709weak_ptr\u6307\u5411\u8be5\u5bf9\u8c61&#xff0c;\u5bf9\u8c61\u90fd\u4f1a\u88ab\u5220\u9664\u3002\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u4f5c\u4e3a\u4e00\u4e2a\u65c1\u89c2\u8005\u76d1\u89c6shared_ptr\u4e2d\u7ba1\u7406\u7684\u8d44\u6e90\u662f\u5426\u5b58\u5728&#xff0c;\u89e3\u51b3shared_ptr\u53ef\u80fd\u5bfc\u81f4\u7684\u5faa\u73af\u5f15\u7528\u95ee\u9898\u3002<\/p>\n<p>\u5728\u8fd9\u90e8\u5206\u5229\u7528\u7387\u5c40\u90e8\u53d8\u91cf\u7684\u7279\u6027&#xff0c;C&#043;&#043;\u7684\u5c40\u90e8\u53d8\u91cf\u5b58\u5728\u6808\u4e2d&#xff0c;\u5f53\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\u540e&#xff0c;\u90a3\u6808\u4f1a\u81ea\u52a8\u91ca\u653e\u7a7a\u95f4\u3002\u800c\u667a\u80fd\u6307\u9488\u540c\u6837\u4e3a\u5c40\u90e8\u53d8\u91cf&#xff0c;\u5b58\u5728\u6808\u4e2d\u3002<\/p>\n<h5 id=\"2.4.2%20%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95\">&#x1f9e9;2.4.2 \u57fa\u672c\u7528\u6cd5<\/h5>\n<p><span style=\"color:#4da8ee\">\u6837\u4f8b1&#xff1a;&#xff08;\u521d\u6982&#xff09;<\/span><\/p>\n<p>\/\/ \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014 weadk_ptr \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<br \/>\nint main()<br \/>\n{<br \/>\n\/\/ weak_ptr\u4e0d\u652f\u6301\u7ba1\u7406\u8d44\u6e90&#xff0c;\u4e0d\u652f\u6301RAII<br \/>\n\/\/std::weak_ptr&lt;Date&gt; wp1(new Date);<\/p>\n<p>weak_ptr&lt;Date&gt; wp;<br \/>\nshared_ptr&lt;Date&gt; sp; \/\/\u4fdd\u8bc1\u4e0d\u8fc7\u671f<br \/>\n{<br \/>\nshared_ptr&lt;Date&gt; n1(new Date);<br \/>\nwp &#061; n1;<br \/>\ncout &lt;&lt; wp.expired() &lt;&lt; endl;<br \/>\nn1-&gt;_day&#043;&#043;;<br \/>\n\/\/sp &#061; wp.lock();<\/p>\n<p>}<br \/>\n\/\/ \u51fa\u4e86\u4f5c\u7528\u57df\u5c31\u5931\u6548\u4e86<br \/>\ncout &lt;&lt; wp.expired() &lt;&lt; endl;<\/p>\n<p>} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"105\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042657-68393391b4eee.png\" width=\"656\" \/><\/p>\n<p><span style=\"color:#4da8ee\">\u6837\u4f8b2&#xff1a;&#xff08;\u8be6\u7ec6&#xff09;<\/span><\/p>\n<p>std::weak_ptr\u5e95\u5c42\u4e0estd::shared_ptr\u5171\u4eab\u76f8\u540c\u7684\u5f15\u7528\u8ba1\u6570\u673a\u5236&#xff0c;\u4f46\u4e0d\u4f1a\u589e\u52a0\u8ba1\u6570\u3002<\/p>\n<p>int main()<br \/>\n{<br \/>\n\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-  \u4e00&#xff0c;\u521d\u59cb\u5316\u667a\u80fd\u6307\u9488weak_ptr  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/<br \/>\n    \/\/ weak_ptr\u4e0d\u652f\u6301\u7ba1\u7406\u8d44\u6e90&#xff0c;\u4e0d\u652f\u6301RAII<br \/>\nshared_ptr&lt;int&gt; sp(new int(2));<\/p>\n<p>weak_ptr&lt;int&gt; wp1;; \/\/\u6784\u9020\u4e86\u4e00\u4e2a\u7a7aweak_ptr\u5bf9\u8c61<br \/>\nweak_ptr&lt;int&gt; wp2(wp1); \/\/\u901a\u8fc7\u4e00\u4e2a\u7a7aweak_ptr\u5bf9\u8c61\u6784\u9020\u4e86\u53e6\u4e00\u4e2a\u7a7aweak_ptr\u5bf9\u8c61<br \/>\nweak_ptr&lt;int&gt; wp3(sp); \/\/\u901a\u8fc7\u4e00\u4e2ashared_ptr\u5bf9\u8c61\u6784\u9020\u4e86\u4e00\u4e2a\u53ef\u7528\u7684weak_ptr\u5b9e\u4f8b\u5bf9\u8c61<\/p>\n<p>weak_ptr&lt;int&gt; wp4;<br \/>\nwp4 &#061; sp; \/\/\u901a\u8fc7\u4e00\u4e2ashared_ptr\u5bf9\u8c61\u6784\u9020\u4e86\u4e00\u4e2a\u53ef\u7528\u7684weak_ptr\u5b9e\u4f8b\u5bf9\u8c61&#xff08;\u8fd9\u662f\u4e00\u4e2a\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362&#xff09;<\/p>\n<p>weak_ptr&lt;int&gt; wp5;<br \/>\nwp5 &#061; wp3; \/\/\u901a\u8fc7\u4e00\u4e2aweak_ptr\u5bf9\u8c61\u6784\u9020\u4e86\u4e00\u4e2a\u53ef\u7528\u7684weak_ptr\u5b9e\u4f8b\u5bf9\u8c61<\/p>\n<p>\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-  \u4e8c&#xff0c;weak_ptr\u5e38\u7528\u51fd\u6570  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/<\/p>\n<p>\/\/ 1.\u901a\u8fc7\u8c03\u7528weak_ptr\u7c7b\u63d0\u4f9b\u7684use_count()\u65b9\u6cd5\u53ef\u4ee5\u83b7\u5f97\u5f53\u524d\u5171\u4eab\u8be5\u8d44\u6e90\u7684shared_ptr\u6570\u91cf<br \/>\ncout &lt;&lt; &#034;wp1\u7ba1\u7406\u7684shared_ptr\u5185\u5b58\u5f15\u7528\u8ba1\u6570&#xff1a;&#034; &lt;&lt; wp1.use_count() &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;wp2\u7ba1\u7406\u7684shared_ptr\u5185\u5b58\u5f15\u7528\u8ba1\u6570&#xff1a;&#034; &lt;&lt; wp2.use_count() &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;wp3\u7ba1\u7406\u7684shared_ptr\u5185\u5b58\u5f15\u7528\u8ba1\u6570&#xff1a;&#034; &lt;&lt; wp3.use_count() &lt;&lt; endl;<\/p>\n<p>cout &lt;&lt; &#034;wp4\u7ba1\u7406\u7684shared_ptr\u5185\u5b58\u5f15\u7528\u8ba1\u6570&#xff1a;&#034; &lt;&lt; wp4.use_count() &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;wp5\u7ba1\u7406\u7684shared_ptr\u5185\u5b58\u5f15\u7528\u8ba1\u6570&#xff1a;&#034; &lt;&lt; wp5.use_count() &lt;&lt; endl;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; endl;<\/p>\n<p>\/\/ 2.\u901a\u8fc7\u8c03\u7528std::weak_ptr\u7c7b\u63d0\u4f9b\u7684expired()\u65b9\u6cd5\u6765\u5224\u65ad<br \/>\n\/\/\u5224\u65ad\u6307\u9488\u6240\u6307\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u5426\u88ab\u91ca\u653e\u6389 \/ \u6307\u9488\u662f\u5426\u4e3a\u7a7a \/ \u662f\u5426\u8fd8\u6709shared_ptr\u6307\u9488\u6307\u5411weak_ptr\u6307\u5411\u7684\u5185\u5b58\u7a7a\u95f4<br \/>\nshared_ptr&lt;int&gt; sp2 &#061; make_shared&lt;int&gt;(10);<br \/>\nweak_ptr&lt;int&gt; wp6(sp2); \/\/ shared_ptr\u521d\u59cb\u5316weak_ptr<br \/>\nsp2.reset(new int); \/\/ \u6b64\u65f6&#xff0c;\u5df2\u6ca1\u6709\u4e00\u4e2ashared_ptr\u6307\u9488\u6307\u5411weak_ptr\u6307\u5411\u7684\u5185\u5b58\u533a\u57df<br \/>\ncout &lt;&lt; &#034;\u662f\u5426\u5df2\u6ca1\u6709shared_ptr\u6307\u9488\u6307\u5411\u8be5\u5185\u5b58\u533a\u57df&#xff1a;&#034; &lt;&lt; wp6.expired() &lt;&lt; endl;<br \/>\nshared_ptr&lt;int&gt; sp22 &#061; nullptr;<br \/>\nweak_ptr&lt;int&gt; wp7(sp22);<br \/>\ncout &lt;&lt; &#034;weak_ptr\u6307\u9488\u662f\u5426\u4e3a\u7a7a&#xff1a;&#034; &lt;&lt; wp7.expired() &lt;&lt; endl;<\/p>\n<p>\/\/ 3.\u901a\u8fc7\u8c03\u7528weak_ptr\u7c7b\u63d0\u4f9b\u7684lock()\u65b9\u6cd5\u6765\u83b7\u53d6\u7ba1\u7406\u6240\u76d1\u6d4b\u8d44\u6e90\u7684shared_ptr\u5bf9\u8c61,\u8fd4\u56de\u4e00\u4e2ashared_ptr&#xff0c;\u589e\u52a0\u5f15\u7528\u8ba1\u6570<br \/>\nshared_ptr&lt;int&gt; sp3 &#061; wp3.lock(); \/\/ \u5229\u7528\u8fd4\u56de\u7684shared_ptr\u521d\u59cb\u5316<br \/>\ncout &lt;&lt; &#034;share_ptr\u7684\u521d\u59cb\u5316\u4e3a&#xff1a;&#034; &lt;&lt;*sp3 &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;wp3\u7ba1\u7406\u7684shared_ptr\u5185\u5b58\u5f15\u7528\u8ba1\u6570&#xff1a;&#034; &lt;&lt; wp3.use_count() &lt;&lt; endl;<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<h5 id=\"2.4.3%20%E5%AF%B9%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8%E7%9A%84%E8%A7%A3%E5%86%B3\">&#x1f9e9;2.4.3 \u5bf9\u5faa\u73af\u5f15\u7528\u7684\u89e3\u51b3<\/h5>\n<p>\u5047\u5982\u5b58\u5728\u8fd9\u6837\u7684\u4e00\u4e2a\u7ed3\u6784\u4f53<\/p>\n<p>struct ListNode{<br \/>\nint _data;<br \/>\nshared_ptr&lt;ListNode&gt; _next;<br \/>\nshared_ptr&lt;ListNode&gt; _prev;<br \/>\n\/\/\u7528weak_ptr\u5c31\u53ef\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u9732<br \/>\n\/\/weak_ptr&lt;ListNode&gt; _next;<br \/>\n\/\/weak_ptr&lt;ListNode&gt; _prev;<br \/>\n~ListNode()<br \/>\n{<br \/>\ncout &lt;&lt; &#034;~ListNode()&#034; &lt;&lt; endl;<br \/>\n}<br \/>\n}; <\/p>\n<p>\u00a0\u60c5\u51b5\u4e00&#xff1a;\u5f53\u6211\u4eec\u5728shaed_ptr\u7684\u6b63\u5e38\u60c5\u51b5\u4e0b\u672a\u51fa\u73b0\u5185\u5b58\u6cc4\u9732<\/p>\n<p>int main()<br \/>\n{<br \/>\n\/\/ \u4e0b\u9762\u8fd9\u5c31\u662f\u5faa\u73af\u5f15\u7528 &#8211;&gt; \u5185\u5b58\u6cc4\u9732<br \/>\nstd::shared_ptr&lt;ListNode&gt; n1(new ListNode);<br \/>\nstd::shared_ptr&lt;ListNode&gt; n2(new ListNode);<\/p>\n<p>n1-&gt;_next &#061; n2;<br \/>\n\/\/n2-&gt;_prev &#061; n1; \/\/\u4e92\u76f8\u6307\u5411&#xff0c;\u5c31\u4f1a\u51fa\u73b0\u5185\u5b58\u6cc4\u9732<\/p>\n<p>cout &lt;&lt; n1.use_count() &lt;&lt; endl;<br \/>\ncout &lt;&lt; n2.use_count() &lt;&lt; endl;<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"227\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042657-68393391c3ce3.png\" width=\"1200\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"139\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042657-68393391e72e9.png\" width=\"1032\" \/><\/p>\n<p>\u60c5\u51b5\u4e8c&#xff1a;\u5f53\u6211\u4eec\u589e\u52a0\u4e00\u53e5n2-&gt;_prev &#061; n1; \u5230\u51fd\u6570\u4e2d&#xff0c;\u6b64\u65f6\u5c31\u51fa\u73b0\u4e86\u4e92\u76f8\u6307\u5411&#xff0c;\u5bfc\u81f4\u4e86\u5185\u5b58\u6cc4\u9732<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"387\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042658-683933920e9c0.png\" width=\"1200\" \/><\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"121\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042658-6839339242cec.png\" width=\"1200\" \/><\/p>\n<p>\u60c5\u51b5\u4e09&#xff1a;\u9020\u6210\u95ee\u9898\u7684\u672c\u8d28\u662f\u5f15\u7528\u8ba1\u6570\u6c38\u4e0d\u4e3a0&#xff0c;\u90a3\u4e48\u53ea\u8981\u5c06\u5176\u4e2d\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u6539\u4e3aweak_ptr\u5373\u53ef&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"129\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042658-683933925c873.png\" width=\"899\" \/><\/p>\n<h5 id=\"2.4.4%20%E4%B8%8Eshared_ptr%E7%9A%84%E5%85%B3%E7%B3%BB\">&#x1f9e9;2.4.4 \u4e0eshared_ptr\u7684\u5173\u7cfb<\/h5>\n<p>&#x1f338;\u7531\u4e0a\u9762\u53ef\u77e5&#xff1a;weak_ptr\u662f\u4e3a\u4e86\u914d\u5408shared_ptr\u800c\u5f15\u5165\u7684\u4e00\u79cd\u667a\u80fd\u6307\u9488&#xff0c;\u5b83\u6307\u5411\u4e00\u4e2a\u7531shared_ptr\u7ba1\u7406\u7684\u5bf9\u8c61\u800c\u4e0d\u5f71\u54cd\u6240\u6307\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002\u4e5f\u5c31\u662f\u5c06\u4e00\u4e2aweak_ptr\u7ed1\u5b9a\u5230\u4e00\u4e2ashared_ptr\u4e0d\u4f1a\u6539\u53d8shared_ptr\u7684\u5f15\u7528\u8ba1\u6570\u3002<\/p>\n<p>&#x1f338;\u5b83\u53ea\u53ef\u4ee5\u4ece\u4e00\u4e2a shared_ptr \u6216\u53e6\u4e00\u4e2a weak_ptr \u5bf9\u8c61\u6784\u9020&#xff0c;\u5b83\u7684\u6784\u9020\u548c\u6790\u6784\u4e0d\u4f1a\u5f15\u8d77\u5f15\u7528\u8bb0\u6570\u7684\u589e\u52a0\u6216\u51cf\u5c11\u3002\u5f31\u5f15\u7528\u80fd\u68c0\u6d4b\u5230\u6240\u7ba1\u7406\u7684\u5bf9\u8c61\u662f\u5426\u5df2\u7ecf\u88ab\u91ca\u653e&#xff0c;\u4ece\u800c\u907f\u514d\u8bbf\u95ee\u975e\u6cd5\u5185\u5b58\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4&#xff0c;weak_ptr\u662f\u4e3a\u4e86\u5f25\u8865shared_ptr\u5faa\u73af\u5f15\u7528\u800c\u751f\u7684&#xff0c;\u5b83\u6ca1\u6709RAII\u7684\u7279\u6027&#xff0c;\u4e0d\u76f4\u63a5\u7ba1\u7406\u8d44\u6e90&#xff0c;\u53ea\u662fshared_ptr\u7684\u8ddf\u73ed&#xff0c;\u8fd9\u4e5f\u662fweak_ptr\u652f\u6301\u4f7f\u7528shared_ptr\u6784\u9020\u7684\u539f\u56e0\u3002<\/p>\n<p>\u5728\u4f7f\u7528\u4e0a&#xff0c;weak_ptr\u652f\u6301\u6307\u9488\u6240\u6709\u7684\u64cd\u4f5c\u3002\u5b83\u4e0d\u662f\u4e00\u4e2a\u529f\u80fd\u578b\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u800c\u662f\u8f85\u52a9\u578b&#xff0c;\u5b83\u7684\u4f7f\u547d\u662f\u89e3\u51b3shared_ptr\u9020\u6210\u7684\u5faa\u73af\u5f15\u7528\u95ee\u9898\u3002<\/p>\n<p>&#x1f338;\u4e0e shared_ptr \u4e0d\u540c&#xff0c;weak_ptr \u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u6240\u6307\u5411\u7684\u5bf9\u8c61\u3002\u8981\u8bbf\u95ee\u5bf9\u8c61&#xff0c;\u9700\u8981\u5148\u8c03\u7528 lock() \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a shared_ptr\u3002\u5982\u679c\u6240\u6307\u5411\u7684\u5bf9\u8c61\u5df2\u7ecf\u88ab\u9500\u6bc1&#xff0c;\u5219 lock() \u65b9\u6cd5\u8fd4\u56de\u7a7a\u6307\u9488\u3002<\/p>\n<\/p>\n<h3 id=\"3.%20%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E7%9A%84%E5%AE%9E%E7%8E%B0\">3. \u667a\u80fd\u6307\u9488\u7684\u5b9e\u73b0<\/h3>\n<p>&#x1f4da;\u770b\u5b8c\u524d\u9762\u5176\u667a\u80fd\u6307\u9488\u7684\u5b9e\u73b0&#xff0c;\u76f8\u4fe1\u5927\u5bb6\u5bf9\u667a\u80fd\u6307\u9488\u4e5f\u6709\u4e2a\u5927\u6982\u7684\u4e86\u89e3\u4e86&#xff0c;\u4e0b\u9762\u6211\u4eec\u6765\u5b9e\u73b0\u90e8\u5206\u7684\u667a\u80fd\u6307\u9488\u5427&#xff0c;\u5982unique_ptr\u3001shared_ptr\u3001weak_ptr.<\/p>\n<p>\u667a\u80fd\u6307\u9488\u7684\u5b9e\u73b0&#xff0c;\u5fc5\u987b\u89e3\u51b3\u4e0b\u9762\u4e09\u4e2a\u95ee\u9898&#xff1a;<\/p>\n<ul>\n<li>RAII&#xff0c;\u5c06\u8d44\u6e90\u4ea4\u7ed9\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406&#xff0c;\u5373\u6784\u9020\u5bf9\u8c61\u65f6\u5f00\u59cb\u83b7\u53d6&#xff08;\u7ba1\u7406&#xff09;\u8d44\u6e90&#xff0c;\u6790\u6784\u5bf9\u8c61\u65f6\u91ca\u653e\u8d44\u6e90&#xff1b;<\/li>\n<li>\u50cf\u771f\u6b63\u7684\u6307\u9488\u4e00\u6837\u4f7f\u7528&#xff1b;<\/li>\n<li>\u652f\u6301\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u7684\u62f7\u8d1d\u3002<\/li>\n<\/ul>\n<p>\u5176\u4e2d\u6700\u5bb9\u6613\u5b9e\u73b0\u7684\u662f\u50cf\u6307\u9488\u4e00\u6837\u4f7f\u7528&#xff0c;\u53ea\u8981\u91cd\u8f7d\u4f7f\u7528\u6307\u9488\u7684\u8fd0\u7b97\u7b26\u5373\u53ef\u3002\u5176\u6b21\u662f\u5b9e\u73b0RAII&#xff0c;\u6700\u540e\u662f\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u7684\u62f7\u8d1d\u3002<\/p>\n<h4 id=\"3.1%20auto_ptr%E6%A8%A1%E6%8B%9F%E5%AE%9E%E7%8E%B0\">3.1 auto_ptr\u6a21\u62df\u5b9e\u73b0<\/h4>\n<p>auto_ptr\u7684\u5b9e\u73b0\u539f\u7406&#xff1a;\u7ba1\u7406\u6743\u8f6c\u79fb\u7684\u601d\u60f3<\/p>\n<ul>\n<li>\n<p>\u6790\u6784\u51fd\u6570&#xff1a;\u9700\u8981\u5bf9\u5b83\u7ba1\u7406\u7684\u6307\u9488\u5224\u7a7a&#xff0c;\u53ea\u6709\u6307\u9488\u975e\u7a7a\u65f6\u624d\u80fd\u5bf9\u5176\u8fdb\u884c\u91ca\u653e\u8d44\u6e90\u64cd\u4f5c&#xff0c;\u91ca\u653e\u8d44\u6e90\u4ee5\u540e\u5bf9\u5176\u7f6e\u7a7a\u3002<\/p>\n<\/li>\n<li>\n<p>\u62f7\u8d1d\u6784\u9020\u51fd\u6570&#xff1a;\u7528\u4f20\u5165\u5bf9\u8c61\u7ba1\u7406\u7684\u5185\u5b58\u8d44\u6e90\u6765\u6784\u9020\u5f53\u524d\u5bf9\u8c61&#xff0c;\u5e76\u5c06\u4f20\u5165\u5bf9\u8c61\u7ba1\u7406\u8d44\u6e90\u7684\u6307\u9488\u7f6e\u7a7a\u3002<\/p>\n<\/li>\n<li>\n<p>\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570&#xff1a;\u5148\u5c06\u5f53\u524d\u5bf9\u8c61\u7ba1\u7406\u7684\u8d44\u6e90\u91ca\u653e&#xff0c;\u7136\u540e\u518d\u63a5\u7ba1\u4f20\u5165\u5bf9\u8c61\u7ba1\u7406\u7684\u8d44\u6e90&#xff0c;\u6700\u540e\u5c06\u4f20\u5165\u5bf9\u8c61\u7ba1\u7406\u8d44\u6e90\u7684\u6307\u9488\u7f6e\u7a7a\u3002<\/p>\n<\/li>\n<\/ul>\n<p>namespace qian {<br \/>\ntemplate&lt;class T&gt;<br \/>\nclass auto_ptr<br \/>\n{<br \/>\npublic:<br \/>\nauto_ptr(T* ptr)<br \/>\n:_ptr(ptr)<br \/>\n{}<br \/>\nauto_ptr(auto_ptr&lt;T&gt;&amp; sp)<br \/>\n:_ptr(sp._ptr)<br \/>\n{<br \/>\n\/\/ \u7ba1\u7406\u6743\u8f6c\u79fb<br \/>\nsp._ptr &#061; nullptr;<br \/>\n}<br \/>\nauto_ptr&lt;T&gt;&amp; operator&#061;(auto_ptr&lt;T&gt;&amp; ap)<br \/>\n{<br \/>\n\/\/ \u68c0\u6d4b\u662f\u5426\u4e3a\u81ea\u5df1\u7ed9\u81ea\u5df1\u8d4b\u503c<br \/>\nif (this !&#061; &amp;ap)<br \/>\n{<br \/>\n\/\/ \u91ca\u653e\u5f53\u524d\u5bf9\u8c61\u4e2d\u8d44\u6e90<br \/>\nif (_ptr)<br \/>\ndelete _ptr;<br \/>\n\/\/ \u8f6c\u79fbap\u4e2d\u8d44\u6e90\u5230\u5f53\u524d\u5bf9\u8c61\u4e2d<br \/>\n_ptr &#061; ap._ptr;<br \/>\nap._ptr &#061; NULL;<br \/>\n}<br \/>\nreturn *this;<br \/>\n}<br \/>\n~auto_ptr()<br \/>\n{<br \/>\nif (_ptr)<br \/>\n{<br \/>\ncout &lt;&lt; &#034;delete:&#034; &lt;&lt; _ptr &lt;&lt; endl;<br \/>\ndelete _ptr;<br \/>\n}<br \/>\n}<br \/>\n\/\/ \u50cf\u6307\u9488\u4e00\u6837\u4f7f\u7528<br \/>\nT&amp; operator*()<br \/>\n{<br \/>\nreturn *_ptr;<br \/>\n}<br \/>\nT* operator-&gt;()<br \/>\n{<br \/>\nreturn _ptr;<br \/>\n}<br \/>\nprivate:<br \/>\nT* _ptr;<br \/>\n}; <\/p>\n<h4 id=\"3.2%C2%A0unique_ptr%E6%A8%A1%E6%8B%9F%E5%AE%9E%E7%8E%B0\">3.2\u00a0unique_ptr\u6a21\u62df\u5b9e\u73b0<\/h4>\n<p>\u6a21\u62df\u5b9e\u73b0\u7684\u8fc7\u7a0b&#xff1a;\u53bb\u9664auto_ptr\u4e2d\u62f7\u8d1d\u548c\u8d4b\u503c\u7684\u51fd\u6570\u3002<\/p>\n<p>&#x1f4da;\u6ce8\u610f&#xff1a;C&#043;&#043;98\u4e2d&#xff0c;delete\u7684\u610f\u601d\u662f\u4e0d\u8ba9\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u9ed8\u8ba4\u51fd\u6570&#xff0c;\u800cC&#043;&#043;11\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e2a\u667a\u80fd\u6307\u9488&#xff0c;\u8d4b\u4e88delete\u4e00\u4e2a\u65b0\u529f\u80fd&#xff1a;\u4e0d\u5141\u8bb8\u8c03\u7528&#xff0c;\u56e0\u6b64\u6211\u4eec\u5728\u4e0b\u9762\u7684\u62f7\u8d1d\u548c\u8d4b\u503c\u4e2d\u7528\u5230\u4e86<span style=\"color:#ff9900\">delete<\/span>\u3002<\/p>\n<p>namespace qian<br \/>\n{<br \/>\n    template&lt;class T&gt;<br \/>\n    class unique_ptr<br \/>\n    {<br \/>\n    public:<br \/>\n        unique_ptr(T* ptr &#061; nullptr)<br \/>\n                :_ptr(ptr)<br \/>\n        {}<br \/>\n        ~unique_ptr()<br \/>\n        {<br \/>\n            if (_ptr !&#061; nullptr)<br \/>\n            {<br \/>\n                cout &lt;&lt; &#034;delete:&#034; &lt;&lt; _ptr &lt;&lt; endl;<br \/>\n                delete _ptr;<br \/>\n                _ptr &#061; nullptr;<br \/>\n            }<br \/>\n        }<br \/>\n        unique_ptr(auto_ptr&lt;T&gt;&amp; ap) &#061; delete;<br \/>\n        unique_ptr&amp; operator&#061;(auto_ptr&lt;T&gt;&amp; ap) &#061; delete;<\/p>\n<p>        T&amp; operator*()<br \/>\n        {<br \/>\n            return *_ptr;<br \/>\n        }<br \/>\n        T* operator-&gt;()<br \/>\n        {<br \/>\n            return _ptr;<br \/>\n        }<br \/>\n    private:<br \/>\n        T* _ptr;<br \/>\n    };<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\n    qian::unique_ptr&lt;int&gt; up1(new int);<br \/>\n    qian::unique_ptr&lt;int&gt; up2(new int);<br \/>\n    up2 &#061; up1; \/\/ error<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4 id=\"3.3%C2%A0shared_ptr%E6%A8%A1%E6%8B%9F%E5%AE%9E%E7%8E%B0\">3.3\u00a0shared_ptr\u6a21\u62df\u5b9e\u73b0<\/h4>\n<ul>\n<li>\u589e\u52a0count\u6210\u5458\u53d8\u91cf&#xff0c;\u8868\u793a\u5f15\u7528\u8ba1\u6570&#xff1b;<\/li>\n<li>\u6784\u9020\u51fd\u6570&#xff1a;\u5f53\u83b7\u53d6\u5230\u8d44\u6e90\u5219\u8bbe\u7f6ecount&#061;1&#xff0c;\u8868\u793a\u5f53\u524d\u53ea\u6709\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u7ba1\u7406\u6b64\u8d44\u6e90&#xff1b;<\/li>\n<li>\u62f7\u8d1d\u6784\u9020\u51fd\u6570&#xff1a;\u5c06\u4f20\u5165\u7684\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u4e2d\u7684count&#043;&#043;&#xff0c;\u8868\u793a\u65b0\u589e\u4e86\u4e00\u4e2a\u7ba1\u7406\u8005&#xff1b;<\/li>\n<li>\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570&#xff1a;\u5c06\u672c\u667a\u80fd\u6307\u9488\u7684count&#8211;&#xff0c;\u8868\u793a\u89e3\u9664\u5bf9\u5f53\u524d\u8d44\u6e90\u7684\u5f15\u7528&#xff0c;\u7136\u540e\u518d\u5c06\u4f20\u5165\u7684\u667a\u80fd<\/li>\n<li>\u6307\u9488\u5bf9\u8c61\u4e2d\u7684count&#043;&#043;&#xff0c;\u8868\u793a\u7ba1\u7406\u65b0\u7684\u8d44\u6e90&#xff1b;<\/li>\n<li>\u6790\u6784\u51fd\u6570&#xff1a;count&#8211;&#xff0c;\u8868\u793a\u89e3\u9664\u5bf9\u5f53\u524d\u7ba1\u7406\u8d44\u6e90\u7684\u5f15\u7528&#xff0c;\u5982\u679ccount&#061;0\u5219\u91ca\u653e\u8d44\u6e90&#xff1b;<\/li>\n<li>\u91cd\u8f7d*\u548c-&gt;\u8fd0\u7b97\u7b26&#xff0c;\u4f7fshared_ptr\u5bf9\u8c61\u5177\u6709\u6307\u9488\u4e00\u6837\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\">\u5176\u4e2d&#xff0c;operator &#061; \u7684\u91cd\u8f7d\u9700\u8981\u6ce8\u610f\u4e24\u4e2a\u95ee\u9898&#xff1a;<\/span><\/p>\n<ul>\n<li>\u5185\u5b58\u6cc4\u6f0f&#xff1a;\u8d4b\u503c\u65f6\u8981\u628a\u81ea\u5df1\u7684\u5f15\u7528\u8ba1\u6570\u7ed9\u5bf9\u65b9&#xff0c;\u8d4b\u503c\u4ee3\u8868\u5bf9\u65b9\u8981\u5171\u540c\u63a5\u7ba1\u81ea\u5df1\u7ba1\u7406\u7684\u8d44\u6e90&#xff0c;\u6240\u4ee5\u5bf9\u65b9\u7684\u5f15\u7528\u8ba1\u6570\u4e5f\u8981 &#8211; 1&#xff1b;<\/li>\n<li>\u81ea\u6211\u8d4b\u503c&#xff1a;\u672c\u8d28\u4e5f\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f&#xff0c;\u81ea\u6211\u8d4b\u503c\u540e\u8d44\u6e90\u7684\u7ba1\u7406\u6743\u5e76\u672a\u53d1\u751f\u53d8\u5316&#xff0c;\u4f46\u662f\u5f15\u7528\u8ba1\u6570\u5374 &#043; 1\u4e86&#xff0c;\u5230\u771f\u6b63\u6700\u540e\u4e00\u4e2a\u5bf9\u8c61\u65f6&#xff0c;\u5f15\u7528\u8ba1\u6570\u4ecd\u4e0d\u4e3a0&#xff08;\u5982\u679c\u81ea\u6211\u8d4b\u503c1\u6b21&#xff0c;\u90a3\u5c31\u662f1&#xff09;&#xff0c;\u9020\u6210\u8d44\u6e90\u4e0d\u80fd\u91ca\u653e&#xff0c;\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p>namespace qian<br \/>\n{<br \/>\n    template&lt;class T&gt;<br \/>\n    class shared_ptr<br \/>\n    {<br \/>\n    public:<br \/>\n        shared_ptr(T* ptr &#061; nullptr)<br \/>\n            :_ptr(ptr)<br \/>\n            , _pcount(new int(1))<br \/>\n        {}<br \/>\n        ~shared_ptr()<br \/>\n        {<br \/>\n            if (&#8211;(*_pcount) &#061;&#061; 0)<br \/>\n            {<br \/>\n                if (_ptr !&#061; nullptr)<br \/>\n                {<br \/>\n                    cout &lt;&lt; &#034;delete:&#034; &lt;&lt; _ptr &lt;&lt; endl;<br \/>\n                    delete _ptr;<br \/>\n                    _ptr &#061; nullptr;<br \/>\n                }<br \/>\n                delete _pcount;<br \/>\n                _pcount &#061; nullptr;<br \/>\n            }<br \/>\n        }<br \/>\n        shared_ptr(shared_ptr&lt;T&gt;&amp; sp)<br \/>\n            :_ptr(sp._ptr)<br \/>\n            , _pcount(sp._pcount)<br \/>\n        {<br \/>\n            (*_pcount)&#043;&#043;;<br \/>\n        }<br \/>\n        shared_ptr&lt;T&gt;&amp; operator&#061;(shared_ptr&lt;T&gt;&amp; sp)<br \/>\n        {<br \/>\n            if (_ptr !&#061; sp._ptr)       \/\/ \u7ba1\u7406\u540c\u4e00\u8d44\u6e90\u7684\u667a\u80fd\u6307\u9488\u8d4b\u503c\u65e0\u610f\u4e49<br \/>\n            {<br \/>\n                if (&#8211;(*_pcount) &#061;&#061; 0) \/\/ \u5c06\u7ba1\u7406\u7684\u8d44\u6e90\u7684\u5f15\u7528\u8ba1\u6570-1<br \/>\n                {<br \/>\n                    cout &lt;&lt; &#034;delete:&#034; &lt;&lt; _ptr &lt;&lt; endl;<br \/>\n                    delete _ptr;<br \/>\n                    delete _pcount;<br \/>\n                }<br \/>\n                _ptr &#061; sp._ptr;       \/\/ \u4e0e\u4f20\u5165\u7684\u667a\u80fd\u6307\u9488\u5171\u4eab\u8d44\u6e90<br \/>\n                _pcount &#061; sp._pcount; \/\/ \u5c06\u81ea\u5df1\u7684\u5f15\u7528\u8ba1\u6570\u548c\u4f20\u5165\u7684\u667a\u80fd\u6307\u9488\u540c\u6b65<br \/>\n                (*_pcount)&#043;&#043;;         \/\/ \u5f15\u7528\u8ba1\u6570&#043;1,\u8868\u793a\u81ea\u5df1\u662f\u65b0\u589e\u7684\u7ba1\u7406\u8005<br \/>\n            }<br \/>\n            return *this;<br \/>\n        }<br \/>\n        \/\/ \u83b7\u53d6\u5f15\u7528\u8ba1\u6570<br \/>\n        int use_count()<br \/>\n        {<br \/>\n            return *_pcount;<br \/>\n        }<br \/>\n        T&amp; operator*()<br \/>\n        {<br \/>\n            return *_ptr;<br \/>\n        }<br \/>\n        T* operator-&gt;()<br \/>\n        {<br \/>\n            return _ptr;<br \/>\n        }<br \/>\n    private:<br \/>\n        T* _ptr;<br \/>\n        int* _pcount; \/\/ \u5f15\u7528\u8ba1\u6570<br \/>\n    };<br \/>\n}<\/p>\n<p>int main(){<br \/>\n    qian::shared_ptr&lt;int&gt; sp1(new int(1));<br \/>\n    cout &lt;&lt; sp1.use_count() &lt;&lt; endl;<br \/>\n    qian::shared_ptr&lt;int&gt; sp2(sp1);<br \/>\n    cout &lt;&lt; sp1.use_count() &lt;&lt; endl;<br \/>\n    cout &lt;&lt; sp2.use_count() &lt;&lt; endl;<\/p>\n<p>    qian::shared_ptr&lt;int&gt; sp3(new int(0));<br \/>\n    cout &lt;&lt; sp3.use_count() &lt;&lt; endl;<\/p>\n<p>    qian::shared_ptr&lt;int&gt; sp4(new int(2));<br \/>\n    sp3 &#061; sp4;<br \/>\n    cout &lt;&lt; sp3.use_count() &lt;&lt; endl;<br \/>\n    cout &lt;&lt; sp4.use_count() &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4 id=\"3.4%C2%A0weak_ptr%E6%A8%A1%E6%8B%9F%E5%AE%9E%E7%8E%B0\">3.4\u00a0weak_ptr\u6a21\u62df\u5b9e\u73b0<\/h4>\n<ul>\n<li>\u6784\u9020\u51fd\u6570&#xff1a;\u65e0\u53c2\u6784\u9020<\/li>\n<li>\u62f7\u8d1d\u6784\u9020&#xff1a;\u652f\u6301\u53c2\u6570\u662fshared_ptr\u7c7b\u578b\u548c\u672c\u8eab\u7c7b\u578b\u6784\u9020&#xff0c;\u540c\u65f6\u63a5\u7ba1shared_ptr\u7ba1\u7406\u7684\u8d44\u6e90&#xff0c;\u4f46\u4e0d\u589e\u52a0\u5f15\u7528\u8ba1\u6570<\/li>\n<li>\u62f7\u8d1d\u8d4b\u503c&#xff1a;\u540c\u4e0a\u3002\u667a\u80fd\u6307\u9488\u4e00\u822c\u6709get()\u63a5\u53e3&#xff0c;\u6240\u4ee5\u8fd4\u56de\u6307\u9488\u65f6\u53ef\u4ee5\u8c03\u7528<\/li>\n<li>\u50cf\u6307\u9488\u4e00\u6837<\/li>\n<\/ul>\n<p>namespace qian<br \/>\n{<br \/>\n    template&lt;class T&gt;<br \/>\n    class weak_ptr<br \/>\n    {<br \/>\n    public:<br \/>\n        weak_ptr()<br \/>\n            :_ptr(nullptr)<br \/>\n        {}<br \/>\n        weak_ptr(const shared_ptr&lt;T&gt;&amp; sp)<br \/>\n            :_ptr(sp.get()) \/\/sp\u7684get\u63a5\u53e3\u5728share_pr\u4e2d\u5df2\u5b9e\u73b0&#xff0c;\u5982\u4e0a<br \/>\n        {}<br \/>\n        weak_ptr&amp; operator&#061;(const shared_ptr&lt;T&gt;&amp; sp)<br \/>\n        {<br \/>\n            \/\/_ptr &#061; sp._shared_ptr;<br \/>\n            _ptr &#061; sp.get();<br \/>\n            return *this;<br \/>\n        }<br \/>\n        T&amp; operator*()<br \/>\n        {<br \/>\n            return *_ptr;<br \/>\n        }<br \/>\n        T* operator-&gt;()<br \/>\n        {<br \/>\n            return _ptr;<br \/>\n        }<br \/>\n    private:<br \/>\n        T* _ptr;<br \/>\n    };<br \/>\n}<\/p>\n<h3><\/h3>\n<hr \/>\n<h3 id=\"%F0%9F%93%96%E6%80%BB%E7%BB%93\">&#x1f4d6;\u603b\u7ed3<\/h3>\n<p>\u4ee5\u4e0a\u5c31\u662f\u667a\u80fd\u6307\u9488\u7684\u5168\u90e8\u5185\u5bb9\u5566&#xff0c;\u540e\u9762\u6211\u4f1a\u5355\u72ec\u51fa\u4e00\u7bc7\u5173\u4e8e\u81ea\u5b9a\u4e49\u5220\u9664\u5668\u7684\u535a\u5ba2&#xff0c;\u656c\u8bf7\u671f\u5f85\u54af&#xff01;&#xff01;&#xff01;<\/p>\n<p>&#x1f49e; &#x1f49e; &#x1f49e;\u90a3\u4e48\u672c\u7bc7\u5230\u6b64\u5c31\u7ed3\u675f&#xff0c;\u5e0c\u671b\u6211\u7684\u8fd9\u7bc7\u535a\u5ba2\u53ef\u4ee5\u7ed9\u4f60\u63d0\u4f9b\u6709\u76ca\u7684\u53c2\u8003\u548c\u542f\u793a&#xff0c;\u611f\u8c22\u5927\u5bb6\u652f\u6301&#xff01;&#xff01;&#xff01;\u795d\u5927\u5bb6\u5929\u5929\u5f00\u5fc3\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042658-6839339270691.gif\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.8k\u6b21\u3002\u4ee5\u4e0a\u5c31\u662f\u667a\u80fd\u6307\u9488\u7684\u5168\u90e8\u5185\u5bb9\u5566\uff0c\u540e\u9762\u6211\u4f1a\u5355\u72ec\u51fa\u4e00\u7bc7\u5173\u4e8e\u81ea\u5b9a\u4e49\u5220\u9664\u5668\u7684\u535a\u5ba2\uff0c\u656c\u8bf7\u671f\u5f85\u54af\uff01\uff01\uff01\ud83d\udc9e \ud83d\udc9e \ud83d\udc9e\uff01\uff01\u795d\u5927\u5bb6\u5929\u5929\u5f00\u5fc3\u3002_\u667a\u80fd\u6307\u9488\u8be6\u89e3<\/p>\n","protected":false},"author":2,"featured_media":40946,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,3757,371,190],"topic":[],"class_list":["post-40962","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-c-","tag-371","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010C++\u9ad8\u9636\u3011\uff1a\u667a\u80fd\u6307\u9488\u7684\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/40962.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010C++\u9ad8\u9636\u3011\uff1a\u667a\u80fd\u6307\u9488\u7684\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.8k\u6b21\u3002\u4ee5\u4e0a\u5c31\u662f\u667a\u80fd\u6307\u9488\u7684\u5168\u90e8\u5185\u5bb9\u5566\uff0c\u540e\u9762\u6211\u4f1a\u5355\u72ec\u51fa\u4e00\u7bc7\u5173\u4e8e\u81ea\u5b9a\u4e49\u5220\u9664\u5668\u7684\u535a\u5ba2\uff0c\u656c\u8bf7\u671f\u5f85\u54af\uff01\uff01\uff01\ud83d\udc9e \ud83d\udc9e \ud83d\udc9e\uff01\uff01\u795d\u5927\u5bb6\u5929\u5929\u5f00\u5fc3\u3002_\u667a\u80fd\u6307\u9488\u8be6\u89e3\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/40962.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-30T04:26:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042655-6839338f8b729.jpg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/40962.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/40962.html\",\"name\":\"\u3010C++\u9ad8\u9636\u3011\uff1a\u667a\u80fd\u6307\u9488\u7684\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-30T04:26:59+00:00\",\"dateModified\":\"2025-05-30T04:26:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/40962.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/40962.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/40962.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010C++\u9ad8\u9636\u3011\uff1a\u667a\u80fd\u6307\u9488\u7684\u5168\u9762\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010C++\u9ad8\u9636\u3011\uff1a\u667a\u80fd\u6307\u9488\u7684\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/40962.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010C++\u9ad8\u9636\u3011\uff1a\u667a\u80fd\u6307\u9488\u7684\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.8k\u6b21\u3002\u4ee5\u4e0a\u5c31\u662f\u667a\u80fd\u6307\u9488\u7684\u5168\u90e8\u5185\u5bb9\u5566\uff0c\u540e\u9762\u6211\u4f1a\u5355\u72ec\u51fa\u4e00\u7bc7\u5173\u4e8e\u81ea\u5b9a\u4e49\u5220\u9664\u5668\u7684\u535a\u5ba2\uff0c\u656c\u8bf7\u671f\u5f85\u54af\uff01\uff01\uff01\ud83d\udc9e \ud83d\udc9e \ud83d\udc9e\uff01\uff01\u795d\u5927\u5bb6\u5929\u5929\u5f00\u5fc3\u3002_\u667a\u80fd\u6307\u9488\u8be6\u89e3","og_url":"https:\/\/www.wsisp.com\/helps\/40962.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-30T04:26:59+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530042655-6839338f8b729.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/40962.html","url":"https:\/\/www.wsisp.com\/helps\/40962.html","name":"\u3010C++\u9ad8\u9636\u3011\uff1a\u667a\u80fd\u6307\u9488\u7684\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-30T04:26:59+00:00","dateModified":"2025-05-30T04:26:59+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/40962.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/40962.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/40962.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010C++\u9ad8\u9636\u3011\uff1a\u667a\u80fd\u6307\u9488\u7684\u5168\u9762\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/40962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=40962"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/40962\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/40946"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=40962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=40962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=40962"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=40962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}