{"id":100689,"date":"2026-09-05T03:33:45","date_gmt":"2026-09-04T19:33:45","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/100689.html"},"modified":"2026-09-05T03:33:45","modified_gmt":"2026-09-04T19:33:45","slug":"%e3%80%90c%e3%80%91%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88%e7%9a%84%e4%bd%bf%e7%94%a8%e5%8f%8a%e5%85%b6%e5%8e%9f%e7%90%86-%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/100689.html","title":{"rendered":"\u3010C++\u3011\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u53ca\u5176\u539f\u7406--\u8be6\u89e3"},"content":{"rendered":"<h2>\u4e00.\u4e3a\u4ec0\u4e48\u6211\u4eec\u4f1a\u9700\u8981\u667a\u80fd\u6307\u9488<\/h2>\n<p>\u56e0\u4e3a\u9700\u8981\u5e2e C&#043;&#043; \u7a0b\u5e8f\u5458\u7ba1\u7406\u5806\u4e0a new \u51fa\u6765\u7684\u5185\u5b58&#xff0c;\u81ea\u52a8\u5b8c\u6210\u91ca\u653e&#xff0c;\u7528\u6765\u89e3\u51b3\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;memory&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u52a8\u6001\u5206\u914d\u5185\u5b58&#xff0c;\u6ca1\u6709\u91ca\u653e\u5c31return<br \/>\nvoid memoryLeak1()<br \/>\n{<br \/>\nstring *str &#061; new string(&#034;\u52a8\u6001\u5206\u914d\u5185\u5b58&#xff01;&#034;);<br \/>\nreturn;<br \/>\n}<\/p>\n<p>\/\/ \u52a8\u6001\u5206\u914d\u5185\u5b58&#xff0c;\u867d\u7136\u5199\u4e86\u91ca\u653e\u5185\u5b58\u7684\u4ee3\u7801&#xff0c;\u534a\u8defreturn\u76f4\u63a5\u8df3\u8fc7<br \/>\nint memoryLeak2()<br \/>\n{<br \/>\nstring *str &#061; new string(&#034;\u5185\u5b58\u6cc4\u9732&#xff01;&#034;);<br \/>\n\/\/ &#8230;<\/p>\n<p>\/\/ \u53d1\u751f\u67d0\u4e9b\u5f02\u5e38&#xff0c;\u9700\u8981\u7ed3\u675f\u51fd\u6570<br \/>\nif (1)<br \/>\n    {<br \/>\nreturn -1;<br \/>\n}<br \/>\n\/\/ \u53e6\u5916&#xff0c;try\u3001catch\u629b\u51fa\u5f02\u5e38\u9000\u51fa\u51fd\u6570&#xff0c;\u540c\u6837\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f&#xff01;<\/p>\n<p>delete str;\/\/ \u91ca\u653e\u4ee3\u7801\u5199\u4e86&#xff0c;\u4f46\u51fd\u6570\u4e2d\u9014\u8fd4\u56de&#xff0c;\u8fd9\u884c\u6839\u672c\u8dd1\u4e0d\u5230<br \/>\nreturn 1;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\nmemoryLeak1();<br \/>\nmemoryLeak2();<\/p>\n<p>return 0;<br \/>\n}<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;memory&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u52a8\u6001\u5206\u914d\u5185\u5b58&#xff0c;\u6ca1\u6709\u91ca\u653e\u5c31return<br \/>\nvoid memoryLeak1()<br \/>\n{<br \/>\nstring *str &#061; new string(&#034;\u52a8\u6001\u5206\u914d\u5185\u5b58&#xff01;&#034;);<br \/>\nreturn;<br \/>\n}<\/p>\n<p>\/\/ \u52a8\u6001\u5206\u914d\u5185\u5b58&#xff0c;\u867d\u7136\u5199\u4e86\u91ca\u653e\u5185\u5b58\u7684\u4ee3\u7801&#xff0c;\u534a\u8defreturn\u76f4\u63a5\u8df3\u8fc7<br \/>\nint memoryLeak2()<br \/>\n{<br \/>\nstring *str &#061; new string(&#034;\u5185\u5b58\u6cc4\u9732&#xff01;&#034;);<br \/>\n\/\/ &#8230;<\/p>\n<p>\/\/ \u53d1\u751f\u67d0\u4e9b\u5f02\u5e38&#xff0c;\u9700\u8981\u7ed3\u675f\u51fd\u6570<br \/>\nif (1)<br \/>\n    {<br \/>\nreturn -1;<br \/>\n}<br \/>\n\/\/ \u53e6\u5916&#xff0c;try\u3001catch\u629b\u51fa\u5f02\u5e38\u9000\u51fa\u51fd\u6570&#xff0c;\u540c\u6837\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f&#xff01;<\/p>\n<p>delete str;\/\/ \u91ca\u653e\u4ee3\u7801\u5199\u4e86&#xff0c;\u4f46\u51fd\u6570\u4e2d\u9014\u8fd4\u56de&#xff0c;\u8fd9\u884c\u6839\u672c\u8dd1\u4e0d\u5230<br \/>\nreturn 1;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\nmemoryLeak1();<br \/>\nmemoryLeak2();<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<p>\u5177\u4f53\u5206\u6790\u5982\u4e0b&#xff1a;<\/p>\n<p>&#xff08;1)memoryLeak1 \u91cc\u9762\u7528 new \u5728\u5806\u4e0a\u5f00\u8f9f string \u5bf9\u8c61&#xff0c;str \u6307\u9488\u672c\u8eab\u5b58\u5728\u6808\u4e0a\u3002\u51fd\u6570\u6267\u884c return \u9000\u51fa\u7684\u65f6\u5019&#xff0c;\u6808\u4e0a\u7684\u5c40\u90e8\u6307\u9488\u53d8\u91cf str \u76f4\u63a5\u9500\u6bc1&#xff0c;<span style=\"color:#fe2c24\">\u4f46\u662f\u5806\u4e0a\u5b9e\u9645\u5b58\u5b57\u7b26\u4e32\u7684\u90a3\u5757\u5185\u5b58\u6ca1\u4eba\u53bb\u91ca\u653e&#xff0c;\u5c31\u9020\u6210\u4e86\u5185\u5b58\u6cc4\u6f0f<\/span>\u3002<\/p>\n<p>(2&#xff09;memoryLeak2 \u867d\u7136\u5199\u597d\u4e86 delete \u7528\u6765\u91ca\u653e\u5185\u5b58\u3002\u4f46 if \u6761\u4ef6\u6210\u7acb&#xff0c;\u76f4\u63a5\u6267\u884c return -1 \u63d0\u524d\u7ed3\u675f\u51fd\u6570&#xff0c;\u540e\u9762\u7684 delete str \u6839\u672c\u8dd1\u4e0d\u5230&#xff0c;\u5185\u5b58\u4f9d\u65e7\u6cc4\u6f0f\u3002<\/p>\n<p>&#xff08;3&#xff09;\u4e0d\u53ea\u662f return \u4f1a\u8df3\u8fc7\u91ca\u653e\u903b\u8f91&#xff0c;\u5982\u679c try \u91cc\u9762\u629b\u51fa\u5f02\u5e38&#xff0c;\u51fd\u6570\u4e2d\u9014\u9000\u51fa&#xff0c;\u540c\u6837\u4f1a\u7ed5\u8fc7 delete&#xff0c;\u5f15\u53d1\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>&#xff08;4&#xff09;main \u51fd\u6570\u8c03\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570&#xff0c;\u4e24\u5904\u90fd\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f&#xff0c;\u5806\u5185\u5b58\u4f1a\u4e00\u76f4\u5360\u7528&#xff0c;\u76f4\u5230\u6574\u4e2a\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u675f\u624d\u88ab\u7cfb\u7edf\u56de\u6536\u3002<\/p>\n<p>\u5982\u679c\u628a\u5806\u4e0a\u5206\u914d\u51fa\u6765\u7684\u5185\u5b58\u4ea4\u7ed9\u666e\u901a\u5bf9\u8c61\u53bb\u6258\u7ba1&#xff0c;\u7b49\u8fd9\u4e2a\u5bf9\u8c61\u751f\u547d\u5468\u671f\u7ed3\u675f&#xff0c;\u5c31\u8c03\u7528\u5b83\u7684\u6790\u6784\u51fd\u6570\u628a\u6258\u7ba1\u7684\u5185\u5b58\u91ca\u653e\u6389&#xff0c;\u8fd9\u4e2a\u601d\u8def\u542c\u8d77\u6765\u5f88\u4e0d\u9519\u3002<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u800c\u667a\u80fd\u6307\u9488\u672c\u8d28\u5c31\u662f\u9760\u8fd9\u5957\u601d\u8def&#xff0c;\u5b9e\u73b0\u5185\u5b58\u81ea\u52a8\u91ca\u653e\u3002<\/span><\/span><\/p>\n<ul>\n<li>C&#043;&#043; \u91cc\u7ba1\u7406\u5806\u5185\u5b58&#xff0c;\u4e3b\u8981\u5c31\u9760 new \u548c delete \u8fd9\u4e00\u7ec4\u8fd0\u7b97\u7b26&#xff1a;<span style=\"color:#fe2c24\"> new&#xff1a;<\/span>\u5728\u5806\u4e0a\u7ed9\u5bf9\u8c61\u5f00\u8f9f\u5185\u5b58&#xff0c;\u8fd4\u56de\u5bf9\u5e94\u7684\u6307\u9488&#xff0c;\u8fd8\u53ef\u4ee5\u987a\u4fbf\u628a\u5bf9\u8c61\u521d\u59cb\u5316\u3002 <span style=\"color:#fe2c24\">delete&#xff1a;<\/span>\u63a5\u6536\u5806\u5bf9\u8c61\u7684\u6307\u9488&#xff0c;\u5148\u9500\u6bc1\u5bf9\u8c61&#xff0c;\u518d\u628a\u8fd9\u5757\u5185\u5b58\u8fd8\u7ed9\u7cfb\u7edf\u3002<\/li>\n<li><span style=\"color:#fe2c24\">\u624b\u52a8\u7ba1\u7406\u52a8\u6001\u5185\u5b58\u5f88\u5bb9\u6613\u51fa\u95ee\u9898<\/span>\u3002\u6700\u96be\u7684\u5c31\u662f\u628a\u63a7\u91ca\u653e\u5185\u5b58\u7684\u65f6\u673a\u3002\u7ecf\u5e38\u51fa\u73b0\u7528\u5b8c\u5185\u5b58\u5fd8\u4e86\u91ca\u653e&#xff0c;\u4e8e\u662f\u5c31\u4ea7\u751f\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e8c.\u5185\u5b58\u6cc4\u6f0f<\/h2>\n<h3>1.\u4ec0\u4e48\u662f\u5185\u5b58\u6cc4\u6f0f&#xff0c;\u5185\u5b58\u6cc4\u6f0f\u7684\u5371\u5bb3<\/h3>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4ec0\u4e48\u662f\u5185\u5b58\u6cc4\u6f0f<\/span><span style=\"background-color:#ffd900\"> <\/span><\/span>\u5185\u5b58\u6cc4\u6f0f\u5c31\u662f\u7a0b\u5e8f\u5199\u5b8c\u4e4b\u540e&#xff0c;\u56e0\u4e3a\u4ee3\u7801\u758f\u5ffd\u6216\u8005\u903b\u8f91 bug&#xff0c;\u7528\u5b8c\u7684\u5806\u5185\u5b58\u6ca1\u6709\u6b63\u5e38\u91ca\u653e\u3002 \u5e76\u4e0d\u662f\u7269\u7406\u5185\u5b58\u51ed\u7a7a\u6d88\u5931\u4e86&#xff0c;\u800c\u662f\u7a0b\u5e8f\u7533\u8bf7\u5230\u4e00\u5757\u5185\u5b58\u4e4b\u540e&#xff0c;\u5f04\u4e22\u4e86\u8fd9\u5757\u5185\u5b58\u7684\u4f7f\u7528\u6743&#xff0c;\u518d\u4e5f\u6ca1\u529e\u6cd5\u628a\u5b83\u8fd8\u7ed9\u7cfb\u7edf&#xff0c;\u8fd9\u5757\u5185\u5b58\u5c31\u88ab\u767d\u767d\u5360\u7528\u6389&#xff0c;\u9020\u6210\u5185\u5b58\u6d6a\u8d39\u3002<\/p>\n<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5185\u5b58\u6cc4\u6f0f\u7684\u5371\u5bb3<\/span><span style=\"background-color:#ffd900\"> <\/span><\/span>\u5bf9\u4e8e\u9700\u8981\u957f\u65f6\u95f4\u8dd1\u7684\u7a0b\u5e8f&#xff0c;\u5185\u5b58\u6cc4\u6f0f\u7684\u5f71\u54cd\u4f1a\u88ab\u653e\u5927&#xff0c;\u50cf\u64cd\u4f5c\u7cfb\u7edf\u670d\u52a1\u3001\u540e\u53f0\u5e38\u9a7b\u7a0b\u5e8f\u8fd9\u7c7b\u3002 \u5185\u5b58\u4e00\u70b9\u70b9\u4e0d\u65ad\u5806\u79ef&#xff0c;\u7a0b\u5e8f\u5360\u7528\u5185\u5b58\u4f1a\u8d8a\u6da8\u8d8a\u9ad8&#xff0c;\u8fd0\u884c\u54cd\u5e94\u53d8\u5f97\u8d8a\u6765\u8d8a\u6162&#xff0c;\u4e25\u91cd\u7684\u65f6\u5019\u76f4\u63a5\u5361\u6b7b\u5d29\u6e83\u3002<\/p>\n<p>\u6765\u770b\u4ee3\u7801&#xff1a;<\/p>\n<p>void MemoryLeaks()<br \/>\n{<br \/>\n    \/\/ 1.\u5185\u5b58\u7533\u8bf7\u4e86\u5fd8\u8bb0\u91ca\u653e<br \/>\n    int* p1 &#061; (int*)malloc(sizeof(int));<br \/>\n    int* p2 &#061; new int;<\/p>\n<p>   \/\/ 2.\u5f02\u5e38\u5b89\u5168\u95ee\u9898<br \/>\n   int* p3 &#061; new int[10];<\/p>\n<p>   Func(); \/\/ \u8fd9\u91ccFunc\u51fd\u6570\u629b\u5f02\u5e38\u5bfc\u81f4 delete[] p3\u672a\u6267\u884c&#xff0c;p3\u6ca1\u88ab\u91ca\u653e.<\/p>\n<p>   delete[] p3;<br \/>\n} <\/p>\n<hr \/>\n<h3>2.\u5185\u5b58\u6cc4\u6f0f\u5206\u7c7b&#xff08;\u4e86\u89e3&#xff09;<\/h3>\n<p>\u5728 C\/C&#043;&#043; \u5f00\u53d1\u91cc&#xff0c;\u4e3b\u8981\u5173\u6ce8\u4e24\u7c7b\u5185\u5b58\u76f8\u5173\u6cc4\u6f0f\u95ee\u9898&#xff1a;<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5806\u5185\u5b58\u6cc4\u6f0f&#xff08;Heap leak&#xff09;<\/span><span style=\"background-color:#ffd900\">\u00a0<\/span><\/span>\u5806\u5185\u5b58\u662f\u7a0b\u5e8f\u8fd0\u884c\u65f6&#xff0c;\u901a\u8fc7malloc\u3001calloc\u3001realloc\u6216\u8005new\u52a8\u6001\u7533\u8bf7\u5f97\u6765\u7684\u5185\u5b58\u3002 \u7528\u5b8c\u4e4b\u540e&#xff0c;\u5fc5\u987b\u914d\u5957\u8c03\u7528free\u6216\u8005delete\u8fdb\u884c\u91ca\u653e\u3002 \u5982\u679c\u4ee3\u7801\u903b\u8f91\u6709\u95ee\u9898&#xff0c;\u7533\u8bf7\u7684\u5806\u5185\u5b58\u6ca1\u6709\u91ca\u653e&#xff0c;\u540e\u7eed\u7a0b\u5e8f\u53c8\u5931\u53bb\u4e86\u5bf9\u8fd9\u5757\u5185\u5b58\u7684\u8bbf\u95ee\u80fd\u529b&#xff0c;\u8fd9\u5757\u7a7a\u95f4\u5c31\u518d\u4e5f\u5f97\u4e0d\u5230\u590d\u7528&#xff0c;\u5c31\u5f62\u6210\u5806\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u7cfb\u7edf\u8d44\u6e90\u6cc4\u6f0f<\/span><\/span> \u7a0b\u5e8f\u5411\u7cfb\u7edf\u7533\u8bf7\u5404\u7c7b\u8d44\u6e90&#xff0c;\u6bd4\u5982\u5957\u63a5\u5b57\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u7ba1\u9053\u7b49\u7b49&#xff0c;\u4f7f\u7528\u7ed3\u675f\u5374\u6ca1\u6709\u8c03\u7528\u5bf9\u5e94\u63a5\u53e3\u91ca\u653e\u3002 \u8fd9\u7c7b\u4e0d\u5c5e\u4e8e\u5806\u5185\u5b58&#xff0c;\u4f46\u540c\u6837\u4f1a\u6301\u7eed\u6d88\u8017\u7cfb\u7edf\u8d44\u6e90\u3002\u79ef\u7d2f\u591a\u4e86\u4f1a\u9020\u6210\u7cfb\u7edf\u8d44\u6e90\u67af\u7aed&#xff0c;\u6574\u4f53\u6027\u80fd\u4e0b\u964d&#xff0c;\u7a0b\u5e8f\u8fd0\u884c\u53d8\u5f97\u4e0d\u7a33\u5b9a<\/p>\n<hr \/>\n<h3>3.\u5982\u4f55\u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f&#xff08;\u4e86\u89e3&#xff09;<\/h3>\n<p>linux\u4e0b\u5185\u5b58\u6cc4\u6f0f\u68c0\u6d4b&#xff1a;Linux\u4e0b\u51e0\u6b3eC&#043;&#043;\u7a0b\u5e8f\u4e2d\u7684\u5185\u5b58\u6cc4\u9732\u68c0\u67e5\u5de5\u5177_c&#043;&#043;\u5185\u5b58\u6cc4\u9732\u5de5\u5177\u5206\u6790-CSDN\u535a\u5ba2<\/p>\n<p>\u5728windows\u4e0b\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177 &#xff1a;VS\u7f16\u7a0b\u5185\u5b58\u6cc4\u6f0f&#xff1a;VLD(Visual LeakDetector)\u5185\u5b58\u6cc4\u9732\u5e93_visual leak detector vs2020-CSDN\u535a\u5ba2<\/p>\n<hr \/>\n<h3>4.\u5982\u4f55\u907f\u514d\u5185\u5b58\u6cc4\u6f0f<\/h3>\n<ul>\n<li>\u2f2f\u7a0b\u524d\u671f\u826f\u597d\u7684\u8bbe\u8ba1\u89c4\u8303&#xff0c;\u517b\u6210\u826f\u597d\u7684\u7f16\u7801\u89c4\u8303&#xff0c;\u7533\u8bf7\u7684\u5185\u5b58\u7a7a\u95f4\u8bb0\u7740\u5339\u914d\u7684\u53bb\u91ca\u653e\u3002ps&#xff1a;\u8fd9\u4e2a\u7406\u60f3\u72b6\u6001\u3002\u4f46\u662f\u5982\u679c\u78b0\u4e0a\u5f02\u5e38\u65f6&#xff0c;\u5c31\u7b97\u6ce8\u610f\u91ca\u653e\u4e86&#xff0c;\u8fd8\u662f\u53ef\u80fd\u4f1a\u51fa\u95ee\u9898\u3002\u9700\u8981\u4e0b\u2f00\u6761\u667a\u80fd\u6307\u9488\u6765\u7ba1\u7406\u624d\u6709\u4fdd\u8bc1\u3002<\/li>\n<li>\u5c3d\u91cf\u4f7f\u2f64\u667a\u80fd\u6307\u9488\u6765\u7ba1\u7406\u8d44\u6e90&#xff0c;\u5982\u679c\u2f83\u2f30\u573a\u666f\u2f50\u8f83\u7279\u6b8a&#xff0c;\u91c7\u2f64RAII\u601d\u60f3\u2f83\u2f30\u9020\u4e2a\u8f6e\u2f26\u7ba1\u7406\u3002<\/li>\n<li>\u00a0\u5b9a\u671f\u4f7f\u2f64\u5185\u5b58\u6cc4\u6f0f\u2f2f\u5177\u68c0\u6d4b&#xff0c;\u5c24\u5176\u662f\u6bcf\u6b21\u9879\u2f6c\u5feb\u4e0a\u7ebf\u524d&#xff0c;\u4e0d\u8fc7\u6709\u4e9b\u2f2f\u5177\u4e0d\u591f\u9760\u8c31&#xff0c;\u6216\u8005\u662f\u6536\u8d39\u3002<\/li>\n<\/ul>\n<p>\u00a0\u603b\u7ed3\u2f00\u4e0b&#xff1a;\u5185\u5b58\u6cc4\u6f0f\u2fae\u5e38\u5e38\u2ec5&#xff0c;\u89e3\u51b3\u2f45\u6848\u5206\u4e3a\u4e24\u79cd&#xff1a;<\/p>\n<p>1.\u4e8b\u524d\u9884\u9632\u578b\u3002\u5982\u667a\u80fd\u6307\u9488\u7b49\u30022.\u4e8b\u540e\u67e5\u9519\u578b\u3002\u5982\u6cc4\u6f0f\u68c0\u6d4b\u2f2f\u5177\u3002<\/p>\n<hr \/>\n<h2>\u4e09.\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u53ca\u539f\u7406<\/h2>\n<h3>1.RAII<\/h3>\n<p>RAII&#xff0c;\u5168\u79f0\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316&#xff08;Resource Acquisition Is Initialization&#xff09;&#xff0c;\u662f\u501f\u52a9\u5bf9\u8c61\u751f\u547d\u5468\u671f\u7ba1\u7406\u5404\u7c7b\u7a0b\u5e8f\u8d44\u6e90\u7684\u6280\u672f\u3002\u50cf\u5806\u5185\u5b58\u3001\u6587\u4ef6\u53e5\u67c4\u3001\u7f51\u7edc\u8fde\u63a5\u3001\u4e92\u65a5\u9501\u8fd9\u4e9b\u8d44\u6e90\u90fd\u53ef\u4ee5\u7528\u8fd9\u5957\u65b9\u5f0f\u7ba1\u7406\u3002\u6838\u5fc3\u903b\u8f91&#xff1a;<span style=\"color:#fe2c24\">\u5bf9\u8c61\u6784\u9020\u7684\u65f6\u5019\u62ff\u5230\u8d44\u6e90&#xff0c;\u5bf9\u8c61\u5b58\u6d3b\u671f\u95f4\u8d44\u6e90\u5c31\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528&#xff0c;\u7b49\u5230\u5bf9\u8c61\u6790\u6784\u7684\u65f6\u5019\u518d\u628a\u8d44\u6e90\u91ca\u653e\u6389<\/span>\u3002\u76f8\u5f53\u4e8e\u628a\u8d44\u6e90\u7684\u7ba1\u7406\u5de5\u4f5c\u4ea4\u7ed9\u5bf9\u8c61\u6765\u8d1f\u8d23\u3002<\/p>\n<p>\u8fd9\u4e48\u505a\u5e26\u6765<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4e24\u4e2a\u597d\u5904&#xff1a;<\/span><\/span><\/p>\n<li>\u4e0d\u7528\u6211\u4eec\u624b\u52a8\u5199\u4ee3\u7801\u53bb\u91ca\u653e\u8d44\u6e90<\/li>\n<li>\u5728\u5bf9\u8c61\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u91cc&#xff0c;\u5b83\u6301\u6709\u7684\u8d44\u6e90\u90fd\u662f\u53ef\u7528\u7684<\/li>\n<p>\u800c\u667a\u80fd\u6307\u9488&#xff0c;\u672c\u8d28\u5c31\u662f<span style=\"color:#fe2c24\">\u7c7b\u6a21\u677f<\/span>\u3002\u53ef\u4ee5\u751f\u6210<span style=\"color:#fe2c24\">\u6258\u7ba1\u4efb\u610f\u7c7b\u578b\u7684\u6307\u9488\u5bf9\u8c61<\/span>&#xff1b;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u751f\u547d\u5468\u671f\u7ed3\u675f\u65f6&#xff0c;\u4f1a\u81ea\u52a8\u6267\u884c\u6790\u6784\u51fd\u6570&#xff0c;\u628a\u5b83\u6307\u5411\u7684\u5806\u5185\u5b58\u91ca\u653e\u6389<\/span><\/span>\u3002<\/p>\n<p>\u770b\u4ee3\u7801\u65f6\u95f4&#xff1a;\u4e0b\u9762\u662f\u667a\u80fd\u6307\u9488\u7684\u57fa\u672c\u6846\u67b6&#xff0c;\u6240\u6709\u7684\u667a\u80fd\u6307\u9488\u7c7b\u6a21\u677f\u4e2d\u90fd\u9700\u8981\u5305\u542b<span style=\"color:#fe2c24\">\u4e00\u4e2a\u6307\u9488\u5bf9\u8c61<\/span>&#xff0c;<span style=\"color:#fe2c24\">\u6784\u9020\u51fd\u6570<\/span>\u548c<span style=\"color:#fe2c24\">\u6790\u6784\u51fd\u6570<\/span>\u3002<\/p>\n<p>template&lt;class T&gt;<br \/>\nclass smartptr \/\/ \u81ea\u5b9a\u4e49\u7684\u667a\u80fd\u6307\u9488<br \/>\n{<br \/>\npublic:<br \/>\n    smartptr (T* _ptr) \/\/ \u6784\u9020\u51fd\u6570<br \/>\n        : ptr(_ptr)<br \/>\n    {}<\/p>\n<p>    ~smartptr () \/\/ \u6790\u6784\u51fd\u6570<br \/>\n    {<br \/>\n        if(ptr!&#061;nullptr)<br \/>\n        {<br \/>\n            cout &lt;&lt; &#034;smartptr: delete&#034; &lt;&lt; endl;<br \/>\n            delete ptr;<br \/>\n            ptr &#061; nullptr;<br \/>\n        }<br \/>\n    }<br \/>\nprivate:<br \/>\n    T* ptr; \/\/ \u6307\u9488\u5bf9\u8c61<br \/>\n}; <\/p>\n<hr \/>\n<h3>2.\u667a\u80fd\u6307\u9488\u7684\u539f\u7406<\/h3>\n<p>\u524d\u9762\u5199\u7684\u7b80\u6613\u8d44\u6e90\u7ba1\u7406\u7c7b(smartptr )&#xff0c;\u8fd8\u7b97\u4e0d\u4e0a\u771f\u6b63\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u5b83\u7f3a\u5c11\u6307\u9488\u8be5\u6709\u7684\u884c\u4e3a\u3002\u666e\u901a\u6307\u9488\u53ef\u4ee5\u89e3\u5f15\u7528&#xff0c;\u4e5f\u80fd\u7528-&gt;\u8bbf\u95ee\u5bf9\u8c61\u7684\u6210\u5458\u3002<\/p>\n<p>\u667a\u80fd\u6307\u9488\u8981\u505a\u5230\u548c\u666e\u901a\u6307\u9488\u7528\u6cd5\u63a5\u8fd1&#xff0c;\u5c31\u8981\u5728\u7c7b\u91cc\u9762\u91cd\u8f7d*\u548c-&gt;\u8fd0\u7b97\u7b26\u3002 \u91cd\u8f7d\u4e4b\u540e&#xff0c;\u5c31\u53ef\u4ee5\u50cf\u666e\u901a\u88f8\u6307\u9488\u4e00\u6837&#xff0c;\u7528*\u89e3\u5f15\u7528\u3001\u7528-&gt;\u8bbf\u95ee\u6258\u7ba1\u5bf9\u8c61\u91cc\u9762\u7684\u6210\u5458\u3002<\/p>\n<p>\u6765\u770b\u4ee3\u7801&#xff1a;<\/p>\n<p>template&lt;class T&gt;<br \/>\nclass smartptr \/\/ \u7b80\u6613\u81ea\u5b9a\u4e49\u667a\u80fd\u6307\u9488<br \/>\n{<br \/>\npublic:<br \/>\n    smartptr (T* _ptr) \/\/ \u6784\u9020\u51fd\u6570<br \/>\n        : ptr(_ptr)<br \/>\n    {}<\/p>\n<p>    ~smartptr () \/\/ \u6790\u6784\u51fd\u6570<br \/>\n    {<br \/>\n        if(ptr!&#061;nullptr)<br \/>\n        {<br \/>\n            cout &lt;&lt; &#034;smartptr: delete&#034; &lt;&lt; endl;<br \/>\n            delete ptr;<br \/>\n            ptr &#061; nullptr;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    T&amp; operator*() {return *ptr;} \/\/ \u91cd\u8f7d*\u8fd0\u7b97\u7b26<\/p>\n<p>    T* operator-&gt;() {return ptr;} \/\/ \u91cd\u8f7d-&gt;\u8fd0\u7b97\u7b26<br \/>\nprivate:<br \/>\n    T* ptr; \/\/ \u4fdd\u5b58\u6258\u7ba1\u7684\u88f8\u6307\u9488<br \/>\n};<\/p>\n<p>\u5f53\u6211\u4eec\u7684\u7a0b\u5e8f\u7ed3\u675f\u65f6&#xff0c;\u4e0b\u9762\u4ee3\u7801\u7684 ptr1 \u548c ptr2 \u6307\u9488\u88ab\u9500\u6bc1&#xff0c;\u90a3\u4e48\u5bf9\u8c61\u00a0ptr1 \u548c ptr2 \u4f1a\u81ea\u52a8\u8c03\u7528\u6790\u6784\u51fd\u6570\u53bb\u91ca\u653e\u6240\u6307\u5411\u7684\u8d44\u6e90&#xff0c;<span style=\"color:#fe2c24\">\u8fd9\u662f\u667a\u80fd\u6307\u9488\u7684\u7279\u70b9\u4f53\u73b0\u51fa\u6765\u4e86<\/span>\u3002<\/p>\n<p>int main()<br \/>\n{<br \/>\n    smartptr&lt;int&gt; ptr1(new int(1)); \/\/ \u6307\u5411int\u7c7b\u578b\u7684\u667a\u80fd\u6307\u9488<br \/>\n    smartptr&lt;string&gt; ptr2(new string(&#034;string&#034;)); \/\/ \u6307\u5411string\u7c7b\u578b\u7684\u667a\u80fd\u6307\u9488<br \/>\n    cout &lt;&lt; *ptr1 &lt;&lt; endl;<br \/>\n    cout &lt;&lt; ptr2-&gt;c_str() &lt;&lt; endl;<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u521b\u5efa ptr1 \u65f6&#xff0c;\u5728\u5806\u4e0a\u5f00\u8f9f\u4e86\u4e00\u4e2a int \u7a7a\u95f4&#xff0c;\u4ea4\u7ed9\u667a\u80fd\u6307\u9488\u7ba1\u7406\u3002ptr1 \u672c\u8eab\u662f\u6808\u5bf9\u8c61&#xff0c;\u751f\u547d\u5468\u671f\u7531\u7cfb\u7edf\u81ea\u52a8\u7ba1\u63a7\u3002<\/p>\n<p>ptr2 \u7684\u539f\u7406\u4e00\u6837&#xff0c;\u8d1f\u8d23\u6258\u7ba1\u5806\u4e0a\u7684 string \u5bf9\u8c61\u3002<\/p>\n<p>\u6253\u5370 *ptr1 \u65f6&#xff0c;\u4f1a\u89e6\u53d1\u6211\u4eec\u91cd\u8f7d\u7684\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26&#xff0c;\u53d6\u51fa\u5806\u4e2d\u4fdd\u5b58\u7684\u6574\u578b\u6570\u503c\u5e76\u8f93\u51fa\u3002<\/p>\n<p>ptr2-&gt;c_str() \u4f1a\u8c03\u7528\u91cd\u8f7d\u7684\u7bad\u5934\u8fd0\u7b97\u7b26&#xff0c;\u62ff\u5230\u5185\u90e8\u7684\u88f8\u6307\u9488&#xff0c;\u8fdb\u800c\u8c03\u7528\u5b57\u7b26\u4e32\u7684 c_str() \u65b9\u6cd5\u6253\u5370\u5185\u5bb9\u3002<\/p>\n<p>\u5f53 main \u51fd\u6570\u6267\u884c\u7ed3\u675f&#xff0c;\u6808\u4e0a\u7684 ptr1\u3001ptr2 \u4f1a\u81ea\u52a8\u9500\u6bc1&#xff0c;\u89e6\u53d1\u6790\u6784\u51fd\u6570&#xff0c;\u81ea\u52a8\u91ca\u653e\u6258\u7ba1\u7684\u5806\u5185\u5b58&#xff0c;\u5168\u7a0b\u4e0d\u9700\u8981\u6211\u4eec\u624b\u52a8\u5199 delete&#xff0c;\u5b8c\u7f8e\u89e3\u51b3\u624b\u52a8\u91ca\u653e\u5185\u5b58\u7684\u9ebb\u70e6\u3002<\/p>\n<p>\u8fd0\u884c\u65f6\u8fd9\u4e24\u4e2a\u4ee3\u7801\u52a0\u4e00\u8d77\u53ef\u4ee5\u8dd1&#xff0c;\u6211\u662f\u4e3a\u4e86\u5206\u6790\u62c6\u5f00\u6765\u770b\u3002<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"150\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193340-6a9b1d14f2d30.png\" width=\"486\" \/><\/p>\n<p>\u63a5\u4e0b\u6765\u770b&#xff0c;\u81ea\u5b9a\u4e49\u7684\u7c7b\u4e2d\u6ca1\u6709\u5b9a\u4e49<span style=\"color:#fe2c24\">\u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/span>\u548c<span style=\"color:#fe2c24\">\u8d4b\u503c\u91cd\u8f7d\u51fd\u6570<\/span>&#xff0c;\u90a3\u4e48\u6211\u4eec\u53ea\u80fd\u8c03\u7528<span style=\"color:#fe2c24\">\u7c7b\u4e2d\u539f\u751f\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u91cd\u8f7d\u51fd\u6570<\/span>\u3002\u53ef\u662f\u8fd9\u6837\u5c31\u4f1a\u53d1\u751f<span style=\"color:#fe2c24\">\u7a0b\u5e8f\u5d29\u6e83<\/span>\u3002\u524d\u9762\u7c7b\u91cc\u9762\u7684\u51fd\u6570\u4e0d\u53d8&#xff0c;\u8fdb\u884c\u5f00\u59cb\u6d4b\u8bd5&#xff1a;<\/p>\n<p> int main()<br \/>\n {<br \/>\n     smartptr&lt;int&gt; ptr1(new int);<br \/>\n     smartptr&lt;int&gt; ptr2(ptr1); \/\/ \u4f7f\u7528\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020<br \/>\n     return 0;<br \/>\n }<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"479\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193341-6a9b1d151582f.png\" width=\"1205\" \/><\/p>\n<p>ptr2 \u548c ptr1 \u6307\u5411\u540c\u4e00\u5757\u5806\u5185\u5b58\u3002ptr2 \u751f\u547d\u5468\u671f\u7ed3\u675f&#xff0c;\u4f1a\u8c03\u7528\u6790\u6784\u628a\u8fd9\u5757\u5185\u5b58\u91ca\u653e&#xff1b;\u7b49\u5230 ptr1 \u518d\u9500\u6bc1\u7684\u65f6\u5019&#xff0c;\u53c8\u4f1a\u6267\u884c\u4e00\u6b21\u6790\u6784&#xff0c;\u518d\u6b21\u91ca\u653e\u540c\u4e00\u5757\u5185\u5b58\u3002<\/p>\n<p>\u7a0b\u5e8f\u9000\u51fa\u7684\u65f6\u5019&#xff0c;ptr2 \u5148\u9500\u6bc1&#xff0c;\u5185\u5b58\u5df2\u7ecf\u88ab\u91ca\u653e\u6389\u3002\u4e4b\u540e ptr1 \u9500\u6bc1&#xff0c;\u8fd8\u8981\u518d\u91ca\u653e\u4e00\u904d\u5df2\u7ecf\u91ca\u653e\u8fc7\u7684\u5185\u5b58\u3002<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u540c\u4e00\u5757\u5185\u5b58\u91cd\u590d\u91ca\u653e&#xff0c;\u7a0b\u5e8f\u76f4\u63a5\u5d29\u6e83<\/span><\/span>\u3002\u53ea\u8981\u4e00\u5757\u5185\u5b58\u5df2\u7ecf\u91ca\u653e\u5b8c\u6bd5&#xff0c;\u518d\u5bf9\u5b83\u6267\u884c delete&#xff0c;\u7a0b\u5e8f\u5c31\u4f1a\u51fa\u9519\u3002<\/p>\n<p>\u6240\u4ee5\u8fd9\u4e2a\u7b80\u6613\u667a\u80fd\u6307\u9488&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u7528\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u7684\u62f7\u8d1d\u6784\u9020\u548c\u8d4b\u503c\u91cd\u8f7d\u3002\u5982\u679c\u8981\u81ea\u5df1\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u3001\u8d4b\u503c\u91cd\u8f7d&#xff0c;\u5c31\u5fc5\u987b\u89e3\u51b3\u8d44\u6e90\u53ea\u4f1a\u88ab\u91ca\u653e\u4e00\u6b21\u8fd9\u4e2a\u6838\u5fc3\u95ee\u9898\u3002<\/p>\n<p>\u7b80\u5355\u603b\u7ed3\u4e00\u4e0b<span style=\"color:#fe2c24\">\u667a\u80fd\u6307\u9488\u7684\u539f\u7406<\/span>&#xff1a;<\/p>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">RAII \u7279\u6027\u3002<\/span><\/span><\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u91cd\u8f7d operator* \u548c opertaor-&gt;&#xff0c;\u5177\u6709\u50cf\u6307\u9488\u4e00\u6837\u7684\u884c\u4e3a<\/span><\/span>\u3002<\/li>\n<hr \/>\n<p>\u63a5\u4e0b\u6765\u4ecb\u7ecd\u4e09\u4e2a\u667a\u80fd\u6307\u9488\u5982\u4e0b&#xff0c;\u540c\u65f6\u5b9e\u73b0\u4e00\u4e0b\u4ed6\u4eec&#xff1a;<\/p>\n<h3>3.std::auto_ptr<\/h3>\n<p>https:\/\/cplusplus.com\/reference\/memory\/auto_ptr\/<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"186\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193341-6a9b1d154c340.png\" width=\"1339\" \/><\/p>\n<p>C&#043;&#043;98 \u6807\u51c6\u5e93\u91cc\u9762\u5c31\u63d0\u4f9b\u8fc7auto_ptr\u667a\u80fd\u6307\u9488\u3002<span style=\"color:#fe2c24\">\u4e3a\u4e86\u89e3\u51b3\u62f7\u8d1d\u65f6\u91cd\u590d\u91ca\u653e\u5185\u5b58\u7684\u95ee\u9898&#xff0c;\u5b83\u7528\u7684\u662f\u7ba1\u7406\u6743\u8f6c\u79fb\u7684\u601d\u8def<\/span>\u3002<\/p>\n<p>\u7b80\u5355\u8bf4&#xff0c;\u628a\u65e7\u5bf9\u8c61\u62f7\u8d1d\u7ed9\u65b0\u5bf9\u8c61\u7684\u65f6\u5019&#xff0c;\u4f1a\u628a\u539f\u6765\u5bf9\u8c61\u5185\u90e8\u7684\u6307\u9488\u7f6e\u4e3anullptr&#xff0c;\u8d44\u6e90\u7ba1\u7406\u6743\u76f4\u63a5\u4ea4\u7ed9\u65b0\u5bf9\u8c61\u3002\u6700\u540e\u5c31\u53ea\u5269\u65b0\u5bf9\u8c61\u6301\u6709\u8fd9\u5757\u5806\u8d44\u6e90&#xff0c;\u65e7\u5bf9\u8c61\u4e0d\u518d\u6307\u5411\u4efb\u4f55\u5185\u5b58&#xff0c;\u5c31\u4e0d\u4f1a\u51fa\u73b0\u91cd\u590d delete \u7684\u95ee\u9898\u3002<\/p>\n<p>int main()<br \/>\n{<br \/>\n    auto_ptr&lt;int&gt; ptr1(new int);<br \/>\n    auto_ptr&lt;int&gt; ptr2(ptr1); \/\/ ptr1\u7f6e\u4e3anullptr<br \/>\n    return 0;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"710\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193341-6a9b1d1566a3e.png\" width=\"606\" \/><\/p>\n<p>\u4f46 auto_ptr \u6709\u5f88\u660e\u663e\u7684\u95ee\u9898\u3002\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u6216\u8005\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b8c\u6210\u7ba1\u7406\u6743\u8f6c\u79fb\u4e4b\u540e&#xff0c;\u5982\u679c\u518d\u53bb\u4f7f\u7528\u539f\u6765\u90a3\u4e2a\u65e7\u5bf9\u8c61&#xff0c;\u7a0b\u5e8f\u5c31\u4f1a\u5d29\u6e83\u3002 \u539f\u56e0\u662f\u8f6c\u79fb\u5b8c\u8d44\u6e90\u540e&#xff0c;\u539f\u6765\u5bf9\u8c61\u5185\u90e8\u6307\u9488\u5df2\u7ecf\u88ab\u7f6e\u6210nullptr&#xff0c;\u5bf9\u7a7a\u6307\u9488\u505a\u89e3\u5f15\u7528\u7b49\u64cd\u4f5c\u5c31\u4f1a\u51fa\u9519\u3002<\/p>\n<p>\u8fd9\u4e2a\u7f3a\u9677\u98ce\u9669\u5f88\u9ad8&#xff0c;\u5f88\u591a\u4f01\u4e1a\u9879\u76ee\u4f1a\u76f4\u63a5\u7981\u7528auto_ptr&#xff0c;C&#043;&#043;11 \u4e4b\u540e\u4e5f\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\u5b83\u3002<\/p>\n<hr \/>\n<p id=\"auto_ptr%E7%9A%84%E6%8B%B7%E8%B4%9D%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E5%92%8C%E8%B5%8B%E5%80%BC%E9%87%8D%E8%BD%BD%E5%87%BD%E6%95%B0%E7%9A%84%E5%AE%9E%E7%8E%B0\">a.auto_ptr \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u91cd\u8f7d\u51fd\u6570\u7684\u5b9e\u73b0<\/p>\n<p>\/\/ \u7ba1\u7406\u6743\u8f6c\u79fb&#xff0c;\u8f6c\u79fb\u5b8c\u6210\u4e4b\u540e\u6e90\u5bf9\u8c61\u5c31\u4e0d\u80fd\u518d\u4f7f\u7528<br \/>\nauto_ptr(auto_ptr ap) \/\/ auto_ptr\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570<br \/>\n    : ptr(ap.ptr)<br \/>\n{<br \/>\n    ap.ptr &#061; nullptr;<br \/>\n}<\/p>\n<p>auto_ptr&lt;T&gt; operator&#061;(auto_ptr&lt;T&gt;&amp; ap) \/\/ auto_ptr\u7684\u8d4b\u503c\u91cd\u8f7d\u51fd\u6570<br \/>\n{<br \/>\n    if(ptr !&#061; ap.ptr)<br \/>\n    {<br \/>\n        ptr &#061; ap.ptr;<br \/>\n        ap.ptr &#061; nullptr;<br \/>\n    }<br \/>\n    return ptr;<br \/>\n}<\/p>\n<p><span style=\"color:#fe2c24\">\u62f7\u8d1d\u6784\u9020&#xff1a;<\/span>\u5f62\u53c2 ap \u662f\u4f20\u503c\u65b9\u5f0f&#xff0c;\u590d\u5236\u4f20\u5165\u7684\u5bf9\u8c61\u3002\u521d\u59cb\u5316\u5217\u8868\u628a ap \u91cc\u9762\u5b58\u7684\u88f8\u6307\u9488\u62ff\u7ed9\u65b0\u5bf9\u8c61&#xff0c;\u518d\u628a\u6e90\u5bf9\u8c61 ap \u7684\u6307\u9488\u7f6e\u6210 nullptr\u3002\u8d44\u6e90\u5c31\u4ea4\u5230\u65b0\u5bf9\u8c61\u624b\u4e0a&#xff0c;\u539f\u6765\u7684\u5bf9\u8c61\u4e0d\u518d\u7ba1\u8fd9\u5757\u5185\u5b58\u3002 \u98ce\u9669&#xff1a;\u62f7\u8d1d\u5b8c\u8981\u662f\u8fd8\u53bb\u64cd\u4f5c\u539f\u6765\u7684\u5bf9\u8c61&#xff0c;\u76f8\u5f53\u4e8e\u64cd\u4f5c\u7a7a\u6307\u9488&#xff0c;\u4ee3\u7801\u76f4\u63a5\u62a5\u9519\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u8d4b\u503c\u91cd\u8f7d operator&#061;&#xff1a;<\/span>\u5148\u5bf9\u6bd4\u4e24\u4e2a\u6307\u9488&#xff0c;\u907f\u514d\u81ea\u5df1\u7ed9\u81ea\u5df1\u8d4b\u503c\u3002\u628a ap \u6301\u6709\u7684\u6307\u9488\u63a5\u7ba1\u8fc7\u6765&#xff0c;\u518d\u628a\u6e90\u5bf9\u8c61 ap \u5185\u90e8\u6307\u9488\u7f6e\u7a7a&#xff0c;\u5b8c\u6210\u8d44\u6e90\u79fb\u4ea4\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u6838\u5fc3\u903b\u8f91&#xff1a;<\/span>\u4e0d\u7ba1\u62f7\u8d1d\u6784\u9020\u8fd8\u662f\u8d4b\u503c&#xff0c;\u8d44\u6e90\u90fd\u4f1a\u8f6c\u7ed9\u65b0\u5bf9\u8c61&#xff0c;\u65e7\u5bf9\u8c61\u76f4\u63a5\u7f6e\u7a7a\u3002\u786e\u4fdd\u540c\u4e00\u5757\u5185\u5b58\u53ea\u4f1a\u91ca\u653e\u4e00\u6b21&#xff0c;\u89e3\u51b3\u91cd\u590d delete \u5bfc\u81f4\u5d29\u6e83\u7684\u95ee\u9898\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u81f4\u547d\u7f3a\u70b9&#xff1a;<\/span>\u62f7\u8d1d\u6216\u8005\u8d4b\u503c\u4e4b\u540e&#xff0c;\u65e7\u5bf9\u8c61\u5c31\u4e3a\u7a7a\u4e86\u3002\u8981\u662f\u4ee3\u7801\u6ca1\u6ce8\u610f&#xff0c;\u7ee7\u7eed\u62ff\u65e7\u5bf9\u8c61\u505a\u89e3\u5f15\u7528\u3001\u7528 -&gt; \u8bbf\u95ee\u6210\u5458&#xff0c;\u5c31\u4f1a\u89e6\u53d1\u7a7a\u6307\u9488\u9519\u8bef&#xff0c;\u7a0b\u5e8f\u5d29\u6e83\u3002\u6240\u4ee5\u5f88\u591a\u4f01\u4e1a\u9879\u76ee\u76f4\u63a5\u4e0d\u8ba9\u7528 auto_ptr\u3002<\/p>\n<hr \/>\n<p>b.auto_ptr \u88ab C&#043;&#043;11 \u629b\u5f03\u7684\u4e3b\u8981\u539f\u56e0<\/p>\n<p>(1)\u590d\u5236\u6216\u8005\u8d4b\u503c\u90fd\u4f1a\u6539\u53d8\u8d44\u6e90\u7684\u6240\u6709\u6743<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n\/\/ auto_ptr\u88abC&#043;&#043;11\u629b\u5f03\u7684\u4e3b\u8981\u539f\u56e0&#xff1a;<br \/>\nauto_ptr&lt;string&gt; p1(new string(&#034;I&#039;m hjq!&#034;));<br \/>\nauto_ptr&lt;string&gt; p2(new string(&#034;I&#039;m age 20.&#034;));<\/p>\n<p>cout &lt;&lt; &#034;p1&#xff1a;&#034; &lt;&lt; p1.get() &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;p2&#xff1a;&#034; &lt;&lt; p2.get() &lt;&lt; endl;<\/p>\n<p>\/\/ p2\u8d4b\u503c\u7ed9p1\u540e&#xff0c;\u9996\u5148p1\u4f1a\u5148\u5c06\u81ea\u5df1\u539f\u5148\u6258\u7ba1\u7684\u6307\u9488\u91ca\u653e\u6389&#xff0c;\u7136\u540e\u63a5\u6536\u6258\u7ba1p2\u6240\u6258\u7ba1\u7684\u6307\u9488<br \/>\n\/\/ \u7136\u540ep2\u6240\u6258\u7ba1\u7684\u6307\u9488\u7f6eNULL&#xff0c;\u4e5f\u5c31\u662fp1\u6258\u7ba1\u4e86p2\u6258\u7ba1\u7684\u6307\u9488&#xff0c;\u800cp2\u653e\u5f03\u4e86\u6258\u7ba1<br \/>\np1 &#061; p2;<br \/>\ncout &lt;&lt; &#034;p1 &#061; p2 \u8d4b\u503c\u540e&#xff1a;&#034; &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;p1&#xff1a;&#034; &lt;&lt; p1.get() &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;p2&#xff1a;&#034; &lt;&lt; p2.get() &lt;&lt; endl;<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<p>1.\u4e00\u5f00\u59cbp1\u3001p2\u5404\u81ea\u6258\u7ba1\u4e00\u5757\u5806\u4e0a\u7684 string \u5185\u5b58&#xff0c;get()\u53ef\u4ee5\u62ff\u5230\u5185\u90e8\u88f8\u6307\u9488\u5730\u5740&#xff0c;\u6253\u5370\u51fa\u4e24\u4e2a\u4e0d\u540c\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<p>2.\u6267\u884cp1 &#061; p2\u8d4b\u503c\u64cd\u4f5c&#xff1a;\u4e00\u5f00\u59cbp1\u3001p2\u5404\u81ea\u6258\u7ba1\u4e00\u5757\u5806\u4e0a\u7684 string \u5185\u5b58&#xff0c;get()\u53ef\u4ee5\u62ff\u5230\u5185\u90e8\u88f8\u6307\u9488\u5730\u5740&#xff0c;\u6253\u5370\u51fa\u4e24\u4e2a\u4e0d\u540c\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<ul>\n<li>\u6267\u884cp1 &#061; p2\u8d4b\u503c\u64cd\u4f5c&#xff1a;<\/li>\n<li>p1 \u5148\u628a\u81ea\u5df1\u539f\u6765\u6301\u6709\u7684\u5185\u5b58\u91ca\u653e&#xff1b;<\/li>\n<li>p1 \u63a5\u7ba1 p2 \u7684\u5806\u6307\u9488&#xff1b;<\/li>\n<li>p2 \u5185\u90e8\u6307\u9488\u88ab\u7f6e\u7a7a&#xff0c;\u5f7b\u5e95\u4ea4\u51fa\u8d44\u6e90\u7ba1\u7406\u6743\u3002<\/li>\n<\/ul>\n<p>3.\u8d4b\u503c\u5b8c\u6210\u4e4b\u540e&#xff0c;p1 \u6b63\u5e38\u6301\u6709\u8d44\u6e90&#xff0c;p2 \u5df2\u7ecf\u53d8\u6210\u7a7a\u667a\u80fd\u6307\u9488\u3002<\/p>\n<p>\u4f46\u662f\u5751\u5c31\u5728\u8fd9\u91cc&#xff1a;\u5982\u679c\u540e\u7eed\u4ee3\u7801\u4e0d\u5c0f\u5fc3\u518d\u53bb\u4f7f\u7528 p2&#xff0c;\u6bd4\u5982\u89e3\u5f15\u7528\u3001\u8c03\u7528\u6210\u5458\u65b9\u6cd5&#xff0c;\u5c31\u7b49\u4e8e\u64cd\u4f5c\u7a7a\u6307\u9488&#xff0c;\u7a0b\u5e8f\u76f4\u63a5\u5d29\u6e83\u3002\u8fd9\u5c31\u662f auto_ptr \u4e0d\u63a8\u8350\u4f7f\u7528\u7684\u5173\u952e\u539f\u56e0\u3002<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"194\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193341-6a9b1d1583b39.png\" width=\"511\" \/><\/p>\n<p>\u6211\u4eec\u4f1a\u53d1\u73b0&#xff0c;p2\u6258\u7ba1\u7684\u6307\u9488\u7ed9\u4e86p1\u6258\u7ba1,p2\u6258\u7ba1\u7684\u6307\u9488NULL<\/p>\n<hr \/>\n<p>&#xff08;2&#xff09;. \u5728 STL \u5bb9\u5668\u4e2d\u4f7f\u7528 auto_ptr \u5b58\u5728\u7740\u91cd\u5927\u98ce\u9669&#xff0c;\u56e0\u4e3a\u5bb9\u5668\u5185\u7684\u5143\u7d20\u5fc5\u987b\u652f\u6301\u53ef\u590d\u5236\u548c\u53ef\u8d4b\u503c<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;vector&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    vector&lt;auto_ptr&lt;string&gt;&gt; vec;<br \/>\n    auto_ptr&lt;string&gt; p3(new string(&#034;I&#039;m p3&#034;));<br \/>\n    auto_ptr&lt;string&gt; p4(new string(&#034;I&#039;m p4&#034;));<\/p>\n<p>    \/\/ \u5fc5\u987b\u4f7f\u7528std::move\u4fee\u9970\u6210\u53f3\u503c&#xff0c;\u624d\u53ef\u4ee5\u8fdb\u884c\u63d2\u5165\u5bb9\u5668\u4e2d<br \/>\n    vec.push_back(std::move(p3));<br \/>\n    vec.push_back(std::move(p4));<\/p>\n<p>    cout &lt;&lt; &#034;vec.at(0)&#xff1a;&#034; &lt;&lt;  *vec.at(0) &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;vec[1]&#xff1a;&#034; &lt;&lt;  *vec[1] &lt;&lt; endl;<\/p>\n<p>    \/\/ \u98ce\u9669&#xff1a;<br \/>\n    vec[0] &#061; vec[1]; \/\/ \u5982\u679c\u8fdb\u884c\u8d4b\u503c&#xff0c;\u5c31\u53c8\u56de\u5230\u4e86\u4e0a\u9762\u4e00\u4e2a\u95ee\u9898\u4e2d<br \/>\n    cout &lt;&lt; &#034;vec.at(0)&#xff1a;&#034; &lt;&lt; *vec.at(0) &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;vec[1]&#xff1a;&#034; &lt;&lt; *vec.at(1) &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u521b\u5efa vector \u5bb9\u5668&#xff0c;\u7528\u6765\u5b58\u653e auto_ptr\u3002\u5b9a\u4e49 p3\u3001p4 \u4e24\u4e2a auto_ptr \u5bf9\u8c61&#xff0c;\u5206\u522b\u7ba1\u7406\u5806\u4e0a\u7684 string \u5bf9\u8c61\u3002<\/p>\n<p>push_back (std::move (p3))&#xff1a;std::move \u628a\u5bf9\u8c61\u8f6c\u6210\u53f3\u503c&#xff0c;\u5b8c\u6210\u7ba1\u7406\u6743\u8f6c\u79fb\u3002\u8d44\u6e90\u4ea4\u7ed9\u5bb9\u5668\u5185\u90e8\u7684\u5143\u7d20\u63a5\u7ba1&#xff0c;\u8fd9\u65f6\u5916\u9762\u7684 p3\u3001p4 \u5185\u90e8\u6307\u9488\u5df2\u7ecf\u53d8\u6210\u7a7a&#xff0c;\u540e\u7eed\u4ee3\u7801\u4e0d\u80fd\u518d\u4f7f\u7528 p3\u3001p4\u3002<\/p>\n<p>\u524d\u9762\u6253\u5370\u5bb9\u5668\u4e2d\u7684\u5143\u7d20&#xff0c;\u8d44\u6e90\u8fd8\u6b63\u5e38&#xff0c;\u53ef\u4ee5\u6b63\u5e38\u89e3\u5f15\u7528\u6253\u5370\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u6267\u884c vec [0] &#061; vec [1];&#xff0c;\u89e6\u53d1 auto_ptr \u7684\u8d4b\u503c\u903b\u8f91&#xff0c;\u53d1\u751f\u7ba1\u7406\u6743\u8f6c\u79fb\u3002vec [0] \u63a5\u7ba1 vec [1] \u6301\u6709\u7684\u5185\u5b58&#xff0c;vec [1] \u5185\u90e8\u6307\u9488\u76f4\u63a5\u88ab\u7f6e\u4e3a nullptr\u3002<\/p>\n<p>\u540e\u9762\u518d\u6253\u5370 * vec [1]&#xff0c;\u7b49\u4e8e\u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528&#xff0c;\u7a0b\u5e8f\u5c31\u4f1a\u5d29\u6e83\u3002<\/p>\n<p>\u8fd9\u5c31\u662f auto_ptr \u653e\u8fdb\u5bb9\u5668\u7684\u81f4\u547d\u95ee\u9898\u3002\u5bb9\u5668\u91cc\u9762\u7684\u5143\u7d20\u4e00\u65e6\u4e92\u76f8\u8d4b\u503c&#xff0c;\u6e90\u5143\u7d20\u5c31\u4f1a\u88ab\u7f6e\u7a7a&#xff0c;\u540e\u9762\u8bbf\u95ee\u8fd9\u4e2a\u6e90\u5143\u7d20\u5c31\u4f1a\u51fa\u9519\u3002\u6240\u4ee5 C&#043;&#043; \u6807\u51c6\u4e0d\u5efa\u8bae\u628a auto_ptr \u5b58\u5230 vector \u8fd9\u7c7b\u5bb9\u5668\u3002<\/p>\n<p>C&#043;&#043;11 \u4e4b\u540e\u7684 unique_ptr \u540c\u6837\u4e0d\u652f\u6301\u62f7\u8d1d&#xff0c;\u53ea\u80fd\u901a\u8fc7 move \u8f6c\u79fb\u6240\u6709\u6743\u3002<\/p>\n<p>\u9519\u8bef\u7684\u8d4b\u503c\u64cd\u4f5c\u4f1a\u76f4\u63a5\u7f16\u8bd1\u62a5\u9519&#xff0c;\u4e0d\u7528\u7b49\u5230\u8fd0\u884c\u9636\u6bb5\u624d\u51fa\u95ee\u9898&#xff0c;\u4ece\u6839\u6e90\u907f\u514d\u4e86\u8fd9\u7c7b\u9690\u60a3\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"175\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193341-6a9b1d1591f3f.png\" width=\"529\" \/><\/p>\n<p>VS2022 \u7684\u8868\u73b0\u5206\u4e24\u79cd\u60c5\u51b5&#xff1a;<\/p>\n<ul>\n<li>Debug \u6a21\u5f0f&#xff1a;\u6253\u5370\u51favec[1]: \u4e4b\u540e&#xff0c;\u7a0b\u5e8f\u505c\u987f\u4e00\u5c0f\u4f1a\u513f\u76f4\u63a5\u5f02\u5e38\u9000\u51fa&#xff0c;\u62a5\u9519\u78010xc0000005\u5185\u5b58\u8bbf\u95ee\u51b2\u7a81&#xff0c;\u7a0b\u5e8f\u5d29\u6e83\u7ec8\u6b62&#xff0c;\u540e\u9762\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u3002<\/li>\n<li>Release \u4f18\u5316\u6a21\u5f0f&#xff1a;\u5b58\u5728\u672a\u5b9a\u4e49\u884c\u4e3a&#xff0c;\u6709\u53ef\u80fd\u770b\u8d77\u6765\u5b8c\u6574\u8dd1\u5b8c&#xff0c;\u6ca1\u6709\u62a5\u9519\u3002\u8fd9\u4e0d\u662f\u4ee3\u7801\u6ca1\u95ee\u9898&#xff0c;\u53ea\u662f\u7f16\u8bd1\u5668\u4f18\u5316\u5e26\u6765\u7684\u5047\u8c61&#xff0c;\u6362\u73af\u5883\u5c31\u4f1a\u968f\u673a\u5d29\u6e83\u3002<\/li>\n<\/ul>\n<hr \/>\n<p>&#xff08;3&#xff09;\u4e0d\u652f\u6301\u5bf9\u8c61\u6570\u7ec4\u7684\u5185\u5b58\u7ba1\u7406<\/p>\n<p>auto_ptr&lt;int[]&gt; array(new int[5]); \/\/ \u7f16\u8bd1\u76f4\u63a5\u62a5\u9519<\/p>\n<p><span style=\"color:#fe2c24\">auto_ptr\u5185\u90e8\u6790\u6784\u51fd\u6570\u7528\u7684\u662fdelete&#xff0c;\u800c\u4e0d\u662fdelete[]\u3002\u6570\u7ec4\u5185\u5b58\u5fc5\u987b\u7528delete[]\u91ca\u653e&#xff0c;\u6240\u4ee5\u5b83\u4ece\u8bbe\u8ba1\u4e0a\u5c31\u7ba1\u4e0d\u4e86\u6570\u7ec4\u3002<\/span><\/p>\n<p>\u4e0a\u9762\u8fd9\u884c\u4ee3\u7801&#xff0c;VS \u7f16\u8bd1\u76f4\u63a5\u62a5\u9519&#xff1a;\u6ca1\u6709\u4e0e\u53c2\u6570\u5217\u8868\u5339\u914d\u7684\u6784\u9020\u51fd\u6570\u3002auto_ptr&lt;int[]&gt;\u6839\u672c\u5c31\u4e0d\u652f\u6301\u8fd9\u79cd\u5199\u6cd5\u3002<\/p>\n<p>\u8fd9\u4e5f\u662fauto_ptr\u88ab\u6dd8\u6c70\u7684\u539f\u56e0\u4e4b\u4e00\u3002C&#043;&#043;11 \u4e4b\u540e\u6539\u7528unique_ptr&#xff0c;\u4e13\u95e8\u652f\u6301\u6570\u7ec4\u7248\u672c&#xff0c;unique_ptr&lt;int[]&gt;\u53ef\u4ee5\u6b63\u5e38\u7ba1\u7406\u6570\u7ec4\u5185\u5b58&#xff0c;\u6790\u6784\u65f6\u81ea\u52a8\u8c03\u7528delete[]\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"980\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193341-6a9b1d15a0348.png\" width=\"990\" \/><\/p>\n<p>\u89e3\u51b3\u529e\u6cd5&#xff1a;C&#043;&#043;11\u9009\u62e9unique_ptr \u6b63\u786e\u7ba1\u7406\u6570\u7ec4\u00a0<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    unique_ptr&lt;int[]&gt; arr(new int[5]);<br \/>\n    arr[0] &#061; 100;<br \/>\n    cout &lt;&lt; arr[0] &lt;&lt; endl;<br \/>\n    \/\/ \u6790\u6784\u81ea\u52a8\u8c03\u7528 delete[]<br \/>\n    return 0;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"470\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193341-6a9b1d15e0665.png\" width=\"735\" \/><\/p>\n<p>uto_ptr\u4e09\u4e2a\u786c\u4f24&#xff1a;<\/p>\n<ul>\n<li>\u62f7\u8d1d \/ \u8d4b\u503c\u540e\u6e90\u5bf9\u8c61\u88ab\u638f\u7a7a&#xff0c;\u518d\u7528\u76f4\u63a5\u5d29\u6e83<\/li>\n<li>\u653e\u8fdb\u5bb9\u5668\u91cc&#xff0c;\u5143\u7d20\u4e92\u76f8\u8d4b\u503c\u4f1a\u628a\u5bf9\u65b9\u7f6e\u7a7a<\/li>\n<li>\u4e0d\u652f\u6301\u6570\u7ec4&#xff0c;\u8fdenew int[5]\u90fd\u7ba1\u4e0d\u4e86<\/li>\n<\/ul>\n<p>\u6240\u4ee5 C&#043;&#043;11 \u76f4\u63a5\u7528unique_ptr\u628a\u5b83\u66ff\u6362\u6389\u4e86\u3002<\/p>\n<hr \/>\n<p>&#xff08;4&#xff09;std::auto_ptr \u7684\u6a21\u62df\u5b9e\u73b0<\/p>\n<p>\u6765\u770b\u4ee3\u7801&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u6a21\u62df\u5b9e\u73b0C&#043;&#043;98\u7684auto_ptr\u667a\u80fd\u6307\u9488<br \/>\ntemplate &lt;class T&gt;<br \/>\nclass auto_ptr<br \/>\n{<br \/>\npublic:<br \/>\n\/\/ \u6784\u9020&#xff1a;\u63a5\u7ba1\u88f8\u6307\u9488&#xff0c;\u4fdd\u5b58\u8d44\u6e90\u5730\u5740<br \/>\nauto_ptr(T* ptr)<br \/>\n:_ptr(ptr)<br \/>\n{}<\/p>\n<p>\/\/ \u62f7\u8d1d\u6784\u9020&#xff1a;\u7ba1\u7406\u6743\u8f6c\u79fb&#xff0c;\u4e0d\u662f\u62f7\u8d1d&#xff0c;\u662f\u628a\u522b\u4eba\u8d44\u6e90\u62a2\u8fc7\u6765<br \/>\nauto_ptr(auto_ptr&lt;T&gt; &amp; sp)<br \/>\n:_ptr(sp._ptr)<br \/>\n{<br \/>\n        \/\/ \u539f\u5bf9\u8c61\u7f6e\u7a7a&#xff0c;\u9632\u6b62\u6790\u6784\u65f6\u4e8c\u6b21\u91ca\u653e\u540c\u4e00\u5757\u5185\u5b58<br \/>\nsp._ptr &#061; nullptr;<br \/>\n}<\/p>\n<p>    \/\/ \u8d4b\u503c\u91cd\u8f7d&#xff0c;\u540c\u6837\u505a\u7ba1\u7406\u6743\u8f6c\u79fb<br \/>\n    auto_ptr&lt;T&gt;&amp; operator&#061;(auto_ptr&lt;T&gt;&amp; sp)<br \/>\n    {<br \/>\n        \/\/ \u68c0\u6d4b\u662f\u5426\u81ea\u5df1\u7ed9\u81ea\u5df1\u8d4b\u503c&#xff0c;\u907f\u514d\u628a\u81ea\u5df1\u8d44\u6e90\u76f4\u63a5\u7f6e\u7a7a<br \/>\n        if (this !&#061; &amp;sp)<br \/>\n        {<br \/>\n            \/\/ \u5f53\u524d\u5bf9\u8c61\u5982\u679c\u6301\u6709\u8d44\u6e90&#xff0c;\u5148\u91ca\u653e\u65e7\u8d44\u6e90<br \/>\n            if (_ptr)<br \/>\n                delete _ptr;<\/p>\n<p>            \/\/ \u628a\u4f20\u5165\u5bf9\u8c61\u7684\u8d44\u6e90\u62ff\u8fc7\u6765<br \/>\n            _ptr &#061; sp._ptr;<br \/>\n            \/\/ \u6e90\u5bf9\u8c61\u6307\u9488\u7f6e\u7a7a&#xff0c;\u4e0d\u8ba9\u5b83\u540e\u7eed\u6790\u6784\u91ca\u653e\u8fd9\u5757\u5185\u5b58<br \/>\n            sp._ptr &#061; NULL;<br \/>\n        }<br \/>\n        return *this;<br \/>\n    }<\/p>\n<p>\/\/ \u6790\u6784&#xff1a;\u91ca\u653e\u4fdd\u5b58\u7684\u5806\u5185\u5b58<br \/>\n~auto_ptr()<br \/>\n    {<br \/>\nif (_ptr)<br \/>\n        {<br \/>\n            cout &lt;&lt; &#034;delete:&#034; &lt;&lt; _ptr &lt;&lt; endl;<br \/>\ndelete _ptr;    \/\/ \u6ce8\u610f&#xff01;\u8fd9\u91cc\u53ea\u7528\u666e\u901adelete&#xff0c;\u4e0d\u652f\u6301\u6570\u7ec4new[]<br \/>\n_ptr &#061; nullptr;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ \u89e3\u5f15\u7528\u91cd\u8f7d&#xff0c;\u5bf9\u5916\u8bbf\u95ee\u6258\u7ba1\u7684\u5bf9\u8c61<br \/>\nT&amp; operator*()<br \/>\n    {<br \/>\nreturn *_ptr;<br \/>\n}<\/p>\n<p>\/\/ -&gt;\u91cd\u8f7d&#xff0c;\u65b9\u4fbf\u8bbf\u95ee\u5bf9\u8c61\u7684\u6210\u5458<br \/>\nT* operator-&gt;()<br \/>\n    {<br \/>\nreturn _ptr;<br \/>\n}<br \/>\nprivate:<br \/>\nT* _ptr; \/\/ \u4fdd\u5b58\u7ba1\u7406\u7684\u5806\u5185\u5b58\u6307\u9488<br \/>\n};<\/p>\n<p><span style=\"color:#fe2c24\">\u6838\u5fc3\u5c31\u662f\u7ba1\u7406\u6743\u8f6c\u79fb<\/span>\u3002\u62f7\u8d1d\u6216\u8005\u8d4b\u503c\u7684\u65f6\u5019&#xff0c;\u4e0d\u4f1a\u590d\u5236\u5806\u4e0a\u7684\u5185\u5b58&#xff0c;\u800c\u662f\u76f4\u63a5\u628a\u8d44\u6e90\u4ece\u65e7\u5bf9\u8c61\u79fb\u4ea4\u5230\u65b0\u5bf9\u8c61&#xff0c;\u65e7\u5bf9\u8c61\u5185\u90e8\u6307\u9488\u76f4\u63a5\u7f6e\u7a7a&#xff0c;\u9632\u6b62\u540c\u4e00\u5757\u5185\u5b58\u88ab\u591a\u6b21 delete\u3002<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u62f7\u8d1d\u6784\u9020\u548c\u8d4b\u503c\u91cd\u8f7d\u7684\u53c2\u6570\u90fd\u662f\u5de6\u503c\u5f15\u7528&#xff0c;\u4f20\u4e0d\u4e86\u4e34\u65f6\u5bf9\u8c61\u3002<\/span><\/span><\/p>\n<p>\u6790\u6784\u51fd\u6570\u5185\u90e8\u8c03\u7528\u7684\u662f\u666e\u901a delete&#xff0c;\u6240\u4ee5\u7ba1\u4e0d\u4e86 new [] \u5f00\u8f9f\u7684\u6570\u7ec4&#xff0c;\u8fd9\u4e5f\u662f auto_ptr \u7684\u4e00\u4e2a\u786c\u4f24\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u6709\u4e2a\u5f88\u9690\u853d\u7684\u5751&#xff1a;<\/span>\u53ea\u8981\u505a\u4e86\u62f7\u8d1d\u6216\u8005\u8d4b\u503c&#xff0c;\u539f\u5148\u90a3\u4e2a\u5bf9\u8c61\u5c31\u53d8\u6210\u7a7a\u6307\u9488\u3002\u5982\u679c\u540e\u9762\u8fd8\u7ee7\u7eed\u7528\u5b83\u53bb\u8c03\u7528 *\u3001-&gt;&#xff0c;\u5c31\u4f1a\u89e6\u53d1\u7a7a\u6307\u9488\u8bbf\u95ee&#xff0c;\u7a0b\u5e8f\u76f4\u63a5\u5d29\u6e83\u3002<\/p>\n<p>\u4e5f\u6ca1\u6709\u505a\u6570\u7ec4\u7279\u5316&#xff0c;\u5199 auto_ptr&lt;int []&gt; \u8fd9\u79cd\u8bed\u6cd5\u76f4\u63a5\u7f16\u8bd1\u62a5\u9519\u3002<\/p>\n<p>\u53cd\u89c2 C&#043;&#043;11 \u7684 unique_ptr&#xff0c;\u76f4\u63a5\u628a\u62f7\u8d1d\u6784\u9020\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\u7ed9\u7981\u7528\u6389&#xff0c;\u53ea\u5141\u8bb8 move \u8f6c\u79fb\u8d44\u6e90\u3002<span style=\"color:#fe2c24\">\u9519\u8bef\u7684\u62f7\u8d1d\u8d4b\u503c\u5728\u7f16\u8bd1\u9636\u6bb5\u5c31\u76f4\u63a5\u62a5\u9519&#xff0c;\u4e0d\u4f1a\u628a\u95ee\u9898\u7559\u5230\u8fd0\u884c\u65f6\u624d\u66b4\u9732\u3002<\/span><\/p>\n<hr \/>\n<h3>4.std::unique_ptr<\/h3>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">C&#043;&#043;11 \u7528\u66f4\u4e25\u8c28\u7684 unique_ptr \u53d6\u4ee3\u4e86 auto_ptr\u3002<\/span><\/span><\/p>\n<p>https:\/\/cplusplus.com\/reference\/memory\/unique_ptr\/<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"211\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193341-6a9b1d15f3d5b.png\" width=\"1359\" \/><\/p>\n<p>a.unique_ptr \u7684\u5b9e\u73b0\u539f\u7406<\/p>\n<p><span style=\"color:#fe2c24\">unique_ptr \u662f C&#043;&#043;11 \u5f15\u5165\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u5b83\u91c7\u7528\u72ec\u5360\u5f0f\u6240\u6709\u6743\u7ba1\u7406\u52a8\u6001\u8d44\u6e90<\/span>\u3002\u4e3a\u4e86\u4fdd\u8bc1\u540c\u4e00\u8d44\u6e90\u53ea\u80fd\u7531\u4e00\u4e2a unique_ptr \u7ba1\u7406&#xff0c;unique_ptr \u7981\u7528\u4e86\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26&#xff0c;\u56e0\u6b64\u4e0d\u80fd\u8fdb\u884c\u62f7\u8d1d\u64cd\u4f5c\u3002<\/p>\n<p>int main()<br \/>\n{<br \/>\n    std::unique_ptr&lt;int&gt; ptr1(new int);<\/p>\n<p>    \/\/ \u62f7\u8d1d\u6784\u9020&#xff1a;\u4e0d\u5141\u8bb8<br \/>\n    \/\/ std::unique_ptr&lt;int&gt; ptr2(ptr1);  \/\/ error<\/p>\n<p>    std::unique_ptr&lt;int&gt; ptr3(new int);<\/p>\n<p>    \/\/ \u62f7\u8d1d\u8d4b\u503c&#xff1a;\u4e0d\u5141\u8bb8<br \/>\n    \/\/ ptr3 &#061; ptr1;  \/\/ error<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p>\u4f46\u662f&#xff0c;unique_ptr \u652f\u6301\u79fb\u52a8\u6784\u9020\u548c\u79fb\u52a8\u8d4b\u503c&#xff0c;\u53ef\u4ee5\u901a\u8fc7 std::move \u8f6c\u79fb\u8d44\u6e90\u7684\u6240\u6709\u6743&#xff1a;<\/p>\n<p>std::unique_ptr&lt;int&gt; ptr1(new int(10));<br \/>\nstd::unique_ptr&lt;int&gt; ptr2(std::move(ptr1)); <\/p>\n<p>unique_ptr \u652f\u6301\u79fb\u52a8\u6784\u9020\u548c\u79fb\u52a8\u8d4b\u503c&#xff0c;\u53ef\u4ee5\u901a\u8fc7 std::move \u8f6c\u79fb\u8d44\u6e90\u7684\u6240\u6709\u6743&#xff1a;\u6b64\u65f6\u8d44\u6e90\u7684\u6240\u6709\u6743\u4ece ptr1 \u8f6c\u79fb\u7ed9 ptr2&#xff0c;ptr1 \u4e0d\u518d\u62e5\u6709\u8be5\u8d44\u6e90\u3002<\/p>\n<p>\u4e00\u53e5\u8bdd&#xff1a;unique_ptr &#061; \u4e0d\u80fd\u62f7\u8d1d&#xff0c;\u53ea\u80fd\u79fb\u52a8&#xff0c;\u72ec\u5360\u8d44\u6e90\u3002<\/p>\n<hr \/>\n<p id=\"unique_ptr%E7%9A%84%E6%8B%B7%E8%B4%9D%E5%87%BD%E6%95%B0%E5%92%8C%E8%B5%8B%E5%80%BC%E9%87%8D%E8%BD%BD%E5%87%BD%E6%95%B0\">b.unique_ptr \u7684\u62f7\u8d1d\u51fd\u6570\u548c\u8d4b\u503c\u91cd\u8f7d\u51fd\u6570<\/p>\n<p>template&lt;class T&gt;<br \/>\nclass unique_ptr<br \/>\n{<br \/>\npublic:<br \/>\n    \/\/unique_ptr\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u91cd\u8f7d\u6784\u9020\u7ed9\u7981\u7528\u6389<br \/>\n    unique_ptr(unique_ptr&amp;) &#061; delete;<br \/>\n    operator&#061;(unique_ptr&amp;) &#061; delete;<br \/>\nprivate:<br \/>\n    T* ptr;<br \/>\n}; <\/p>\n<hr \/>\n<p>c.std::unique_ptr \u7684\u6a21\u62df\u5b9e\u73b0<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\nusing namespace std;<\/p>\n<p>namespace hjq<br \/>\n{<br \/>\n    template &lt;class T&gt;<br \/>\n    class unique_ptr<br \/>\n    {<br \/>\n    public:<br \/>\n        \/\/ \u6784\u9020&#xff0c;\u63a5\u7ba1\u5916\u90e8\u4f20\u8fdb\u6765\u7684\u88f8\u6307\u9488<br \/>\n        unique_ptr(T* ptr)<br \/>\n            :_ptr(ptr)<br \/>\n        {}<\/p>\n<p>        \/\/ \u6790\u6784&#xff0c;\u91ca\u653e\u6258\u7ba1\u7684\u5806\u5185\u5b58<br \/>\n        ~unique_ptr()<br \/>\n        {<br \/>\n            if (_ptr)<br \/>\n            {<br \/>\n                cout &lt;&lt; &#034;delete:&#034; &lt;&lt; _ptr &lt;&lt; endl;<br \/>\n                delete _ptr;<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/ \u91cd\u8f7d\u89e3\u5f15\u7528&#xff0c;\u50cf\u666e\u901a\u6307\u9488\u4e00\u6837\u53d6\u503c<br \/>\n        T&amp; operator*()<br \/>\n        {<br \/>\n            return *_ptr;<br \/>\n        }<\/p>\n<p>        \/\/ \u91cd\u8f7d-&gt;&#xff0c;\u65b9\u4fbf\u8bbf\u95ee\u6258\u7ba1\u5bf9\u8c61\u7684\u6210\u5458<br \/>\n        T* operator-&gt;()<br \/>\n        {<br \/>\n            return _ptr;<br \/>\n        }<\/p>\n<p>        \/\/ \u9632\u62f7\u8d1d&#xff1a;C&#043;&#043;11 delete\u8bed\u6cd5&#xff0c;\u76f4\u63a5\u5220\u6389\u62f7\u8d1d\u6784\u9020<br \/>\n        unique_ptr(const unique_ptr&lt;T&gt;&amp; sp) &#061; delete;<br \/>\n        \/\/ \u5220\u9664\u62f7\u8d1d\u8d4b\u503c&#xff0c;\u7981\u6b62\u5bf9\u8c61\u4e4b\u95f4\u62f7\u8d1d\u8d4b\u503c<br \/>\n        unique_ptr&lt;T&gt;&amp; operator&#061;(const unique_ptr&lt;T&gt;&amp; sp) &#061; delete;<\/p>\n<p>    private:<br \/>\n        T* _ptr; \/\/ \u4fdd\u5b58\u6258\u7ba1\u7684\u5806\u6307\u9488<br \/>\n    };<br \/>\n}<\/p>\n<p>unique_ptr \u6a21\u62df\u5b9e\u73b0&#xff0c;\u653e\u5728\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4 hjq\u00a0\u91cc&#xff0c;\u548c\u6807\u51c6\u5e93\u505a\u533a\u5206\u3002<\/p>\n<p>\u6784\u9020\u63a5\u6536\u88f8\u6307\u9488&#xff0c;\u5c06\u5806\u5185\u5b58\u5730\u5740\u4fdd\u5b58\u5230\u6210\u5458_ptr&#xff0c;\u63a5\u7ba1\u8fd9\u5757\u5806\u8d44\u6e90\u3002<\/p>\n<p>\u6790\u6784\u65f6\u5148\u5224\u65ad\u6307\u9488\u4e0d\u4e3a\u7a7a&#xff0c;\u8c03\u7528 delete \u5b8c\u6210\u5185\u5b58\u91ca\u653e\u3002<\/p>\n<p>\u91cd\u8f7doperator*\u548coperator-&gt;&#xff0c;\u4f7f\u7528\u8d77\u6765\u548c\u666e\u901a\u88f8\u6307\u9488\u8bed\u6cd5\u4e00\u6837\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u6838\u5fc3\u903b\u8f91&#xff1a;<\/span>\u901a\u8fc7&#061;delete\u76f4\u63a5\u5220\u6389\u62f7\u8d1d\u6784\u9020\u548c\u62f7\u8d1d\u8d4b\u503c\u3002\u53ea\u8981\u5199\u62f7\u8d1d\u3001\u8d4b\u503c\u7684\u4ee3\u7801&#xff0c;\u7f16\u8bd1\u5c31\u76f4\u63a5\u62a5\u9519&#xff0c;\u5728\u7f16\u8bd1\u9636\u6bb5\u5c31\u7981\u6b62\u62f7\u8d1d\u64cd\u4f5c\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u5bf9\u6bd4 auto_ptr&#xff1a;<\/span>auto_ptr \u5141\u8bb8\u62f7\u8d1d&#xff0c;\u9760\u8fd0\u884c\u65f6\u505a\u8d44\u6e90\u8f6c\u79fb&#xff0c;\u65e7\u5bf9\u8c61\u4f1a\u88ab\u7f6e\u7a7a&#xff0c;\u95ee\u9898\u8981\u7b49\u5230\u7a0b\u5e8f\u8dd1\u8d77\u6765\u624d\u66b4\u9732\u3002\u800c unique_ptr \u76f4\u63a5\u7981\u6b62\u62f7\u8d1d&#xff0c;\u9519\u8bef\u5728\u7f16\u8bd1\u9636\u6bb5\u5c31\u88ab\u63ea\u51fa\u6765\u3002<\/p>\n<p>\u8fd9\u91cc\u8bb0\u5f97\u8981\u6ce8\u610f&#xff0c;\u8fd9\u4e2a\u7b80\u6613\u7248\u672c\u53ea\u5b9e\u73b0\u4e86\u72ec\u5360\u3001\u7981\u6b62\u62f7\u8d1d&#xff0c;\u6ca1\u6709\u5199\u79fb\u52a8\u6784\u9020\u548c\u79fb\u52a8\u8d4b\u503c&#xff0c;\u6240\u4ee5\u8fd8\u4e0d\u80fd\u7528std::move\u8f6c\u79fb\u6240\u6709\u6743\u3002\u771f\u6b63\u6807\u51c6\u5e93\u91cc\u9762\u7684 unique_ptr \u662f\u652f\u6301 move \u79fb\u52a8\u8bed\u4e49\u7684\u3002<\/p>\n<hr \/>\n<h3>5.std::shared_ptr<\/h3>\n<p>https:\/\/cplusplus.com\/reference\/memory\/shared_ptr\/<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"224\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193342-6a9b1d161c283.png\" width=\"1338\" \/><\/p>\n<p>shared_ptr \u662f C&#043;&#043;11 \u63d0\u4f9b\u7684\u667a\u80fd\u6307\u9488\u3002\u5b83\u652f\u6301\u591a\u4e2a shared_ptr \u540c\u65f6\u7ba1\u7406\u540c\u4e00\u5757\u5806\u8d44\u6e90&#xff0c;\u5185\u90e8\u4f9d\u9760\u5f15\u7528\u8ba1\u6570\u8bb0\u5f55\u5f53\u524d\u6709\u591a\u5c11\u4e2a\u6307\u9488\u5728\u5171\u4eab\u8fd9\u4efd\u8d44\u6e90\u3002\u53ea\u6709\u5f53\u6700\u540e\u4e00\u4e2a\u7ba1\u7406\u8be5\u8d44\u6e90\u7684 shared_ptr \u9500\u6bc1\u65f6&#xff0c;\u5f15\u7528\u8ba1\u6570\u51cf\u5230 0&#xff0c;\u624d\u4f1a\u771f\u6b63\u91ca\u653e\u5806\u5185\u5b58&#xff0c;\u4fdd\u8bc1\u540c\u4e00\u5757\u5185\u5b58\u53ea\u4f1a\u91ca\u653e\u4e00\u6b21&#xff0c;\u907f\u514d\u91cd\u590d\u91ca\u653e\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<hr \/>\n<p>a.\u5b9e\u73b0\u539f\u7406<\/p>\n<p>shared_ptr \u9760\u5f15\u7528\u8ba1\u6570&#xff0c;\u5b9e\u73b0\u591a\u4e2a\u5bf9\u8c61\u5171\u4eab\u540c\u4e00\u4efd\u5806\u8d44\u6e90\u3002\u53ef\u4ee5\u8fd9\u4e48\u7406\u89e3&#xff1a;\u597d\u6bd4\u6559\u5ba4\u4e0b\u73ed\u5173\u95e8&#xff0c;\u8c01\u662f\u6700\u540e\u79bb\u5f00\u7684\u4eba&#xff0c;\u8c01\u8d1f\u8d23\u9501\u95e8\u3002<\/p>\n<p>\u6bcf\u4e2a\u88ab shared_ptr \u7ba1\u7406\u7684\u8d44\u6e90&#xff0c;\u5185\u90e8\u90fd\u4f1a\u914d\u5957\u7ef4\u62a4\u4e00\u4efd\u5f15\u7528\u8ba1\u6570&#xff0c;\u8bb0\u5f55\u5f53\u524d\u6709\u591a\u5c11\u667a\u80fd\u6307\u9488\u5728\u5171\u7528\u8fd9\u5757\u8d44\u6e90\u3002<\/p>\n<p>\u5f53\u67d0\u4e2a shared_ptr \u5bf9\u8c61\u9500\u6bc1&#xff0c;\u8c03\u7528\u6790\u6784\u51fd\u6570&#xff0c;\u4ee3\u8868\u5b83\u4e0d\u518d\u4f7f\u7528\u8fd9\u4efd\u8d44\u6e90&#xff0c;\u5f15\u7528\u8ba1\u6570\u5c31\u51cf 1\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u8ba1\u6570\u51cf\u5230 0&#xff0c;\u8bf4\u660e\u8fd9\u662f\u6700\u540e\u4e00\u4e2a\u6301\u6709\u8d44\u6e90\u7684\u5bf9\u8c61&#xff0c;\u6b64\u65f6\u771f\u6b63\u91ca\u653e\u5806\u5185\u5b58\u3002<\/li>\n<li>\u5982\u679c\u8ba1\u6570\u4e0d\u4e3a 0&#xff0c;\u4ee3\u8868\u8fd8\u6709\u522b\u7684\u667a\u80fd\u6307\u9488\u5728\u4f7f\u7528\u8d44\u6e90&#xff0c;\u5c31\u4e0d\u80fd\u91ca\u653e&#xff0c;\u5426\u5219\u5269\u4e0b\u7684\u6307\u9488\u5c31\u4f1a\u53d8\u6210\u91ce\u6307\u9488\u3002<\/li>\n<\/ul>\n<p>std::shared_ptr::get() \u53ef\u4ee5\u62ff\u5230\u5185\u90e8\u4fdd\u5b58\u7684\u88f8\u6307\u9488&#xff0c;\u8fd4\u56de\u7684\u5c31\u662f\u667a\u80fd\u6307\u9488\u6258\u7ba1\u7684\u5730\u5740\u3002\u5934\u6587\u4ef6\u9700\u8981\u5305\u542b &lt;memory&gt;\u3002<\/p>\n<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4f7f\u7528get()\u8981\u683c\u5916\u5c0f\u5fc3&#xff1a;<\/span><\/span>\u4e00\u65e6\u667a\u80fd\u6307\u9488\u628a\u8d44\u6e90\u91ca\u653e\u6389&#xff0c;get \u62ff\u5230\u7684\u88f8\u6307\u9488\u5c31\u53d8\u6210\u65e0\u6548\u6307\u9488\u3002 get \u4e00\u822c\u53ea\u7528\u6765\u628a\u88f8\u6307\u9488\u4f20\u7ed9\u666e\u901a\u51fd\u6570&#xff0c;\u524d\u63d0\u662f\u5916\u90e8\u4ee3\u7801\u4e0d\u4f1a\u53bb delete \u8fd9\u4e2a\u6307\u9488\u3002\u5343\u4e07\u4e0d\u8981\u62ff get \u7684\u8fd4\u56de\u503c\u53bb\u521d\u59cb\u5316\u6216\u8005\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u667a\u80fd\u6307\u9488&#xff0c;\u4f1a\u9020\u6210\u4e24\u5957\u72ec\u7acb\u7684\u5f15\u7528\u8ba1\u6570&#xff0c;\u6700\u7ec8\u5f15\u53d1\u91cd\u590d\u91ca\u653e\u3002<\/p>\n<p>\u6765\u770b\u4ee3\u7801&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<\/p>\n<p>        shared_ptr&lt;int&gt; ptr6(new int(2));<br \/>\n        cout &lt;&lt; ptr6.get() &lt;&lt; endl;<br \/>\n        shared_ptr&lt;int&gt; ptr7 &#061; ptr6;<br \/>\n        cout &lt;&lt; ptr7.get() &lt;&lt; endl;<br \/>\n        cout &lt;&lt; ptr6.get() &lt;&lt; endl;<br \/>\n        return 0;<\/p>\n<p>} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193342-6a9b1d16399dd.png\" width=\"535\" \/><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"741\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193342-6a9b1d1645556.png\" width=\"893\" \/><\/p>\n<p>\u6211\u4eec\u770b\u4e00\u4e0b\u9500\u6bc1\u8fc7\u7a0b&#xff1a;\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"690\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193342-6a9b1d16a0aea.png\" width=\"1441\" \/><\/p>\n<hr \/>\n<p id=\"3.%20shared_ptrd%E7%9A%84%E5%AE%9E%E7%8E%B0\">b.shared_ptrd \u7684\u5b9e\u73b0<\/p>\n<p>\u8d4b\u503c\u91cd\u8f7d\u7684\u4e09\u79cd\u60c5\u51b5<\/p>\n<p>shared_ptr \u8d4b\u503c\u64cd\u4f5c\u4e3b\u8981\u5206\u4e09\u79cd\u573a\u666f\u5904\u7406&#xff0c;\u907f\u514d\u51fa\u9519&#xff1a;<\/p>\n<li>\u81ea\u8d4b\u503c&#xff08;ptr1 &#061; ptr1&#xff09; \u81ea\u5df1\u7ed9\u81ea\u5df1\u8d4b\u503c&#xff0c;\u6ca1\u6709\u4efb\u4f55\u610f\u4e49&#xff0c;\u76f4\u63a5\u8df3\u8fc7\u6240\u6709\u64cd\u4f5c&#xff0c;\u9632\u6b62\u8bef\u91ca\u653e\u81ea\u8eab\u8d44\u6e90\u3002<\/li>\n<li>\u4e24\u4e2a\u6307\u9488\u6307\u5411\u540c\u4e00\u5757\u7a7a\u95f4&#xff08;ptr2 &#061; ptr1&#xff09; \u5982\u679c ptr1 \u548c ptr2 \u672c\u6765\u5c31\u5171\u4eab\u540c\u4e00\u4efd\u8d44\u6e90&#xff0c;\u5f15\u7528\u8ba1\u6570\u4e0d\u7528\u6539\u52a8&#xff0c;\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u5904\u7406\u3002<\/li>\n<li>\u4e24\u4e2a\u6307\u9488\u6307\u5411\u4e0d\u540c\u7a7a\u95f4&#xff08;ptr2 &#061; ptr1&#xff09; \u8fd9\u662f\u6700\u5e38\u7528\u3001\u4e5f\u662f\u771f\u6b63\u9700\u8981\u5904\u7406\u7684\u60c5\u51b5&#xff1a; \u5148\u8ba9 ptr2 \u91ca\u653e\u81ea\u5df1\u539f\u672c\u6258\u7ba1\u7684\u8d44\u6e90&#xff0c;\u5f15\u7528\u8ba1\u6570\u51cf\u4e00&#xff0c;\u8ba1\u6570\u5f52\u96f6\u5c31\u5f7b\u5e95\u91ca\u653e\u5185\u5b58&#xff1b; \u63a5\u7740\u8ba9 ptr2 \u63a5\u7ba1 ptr1 \u7684\u8d44\u6e90&#xff0c;\u540c\u6b65\u5f15\u7528\u8ba1\u6570&#xff1b; \u6700\u540e\u8ba9 ptr1 \u7684\u8d44\u6e90\u5f15\u7528\u8ba1\u6570\u52a0\u4e00&#xff0c;\u5b8c\u6210\u591a\u6307\u9488\u5171\u4eab\u8d44\u6e90\u3002<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"730\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193343-6a9b1d1735327.png\" width=\"775\" \/><\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">product&#xff0c;\u672c\u610f\u662f\u8d44\u6e90\u5bf9\u8c61&#xff0c;\u4e0d\u662f\u53d8\u91cf\u540d\u3002<\/span><\/span><\/p>\n<p><span style=\"color:#fe2c24\">\u591a\u7ebf\u7a0b\u89d2\u5ea6&#xff1a; <\/span>\u56e0\u4e3a_ptrcount\u6307\u5411\u7684\u8ba1\u6570\u5bf9\u8c61\u5b58\u653e\u5728\u5806\u4e0a&#xff0c;\u6240\u6709\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u5757\u8ba1\u6570\u3002\u591a\u7ebf\u7a0b\u5e76\u53d1\u4fee\u6539\u5f15\u7528\u8ba1\u6570\u65f6&#xff0c;\u4f1a\u51fa\u73b0product&#xff0c;\u672c\u610f\u662f\u8d44\u6e90\u5bf9\u8c61&#xff0c;\u6839\u672c\u4e0d\u662f\u53d8\u91cf\u540d\u3002\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u4fee\u6539_ptrcount\u5fc5\u987b\u52a0\u9501&#xff0c;\u4fdd\u8bc1\u8ba1\u6570\u6570\u636e\u4e0d\u4f1a\u51fa\u9519\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u90a3operator*\u8fd4\u56de\u6258\u7ba1\u5bf9\u8c61&#xff0c;\u4e3a\u4ec0\u4e48\u667a\u80fd\u6307\u9488\u5185\u90e8\u4e0d\u7528\u52a0\u9501\u4fdd\u62a4&#xff1f;<\/span><\/p>\n<p>operator*\u62ff\u5230\u7684\u662f\u4e1a\u52a1\u8d44\u6e90\u672c\u8eab&#xff0c;\u8fd9\u5757\u8d44\u6e90\u4f1a\u5b58\u5728\u8bfb\u3001\u5199\u5e76\u53d1&#xff0c;\u8fd9\u90e8\u5206\u4e0d\u5c5e\u4e8e\u667a\u80fd\u6307\u9488\u7684\u804c\u8d23\u8303\u56f4&#xff0c;\u5e76\u53d1\u5b89\u5168\u9700\u8981\u8c03\u7528\u65b9\u81ea\u5df1\u53bb\u4fdd\u8bc1\u3002<\/p>\n<p>\u6765\u770b\u4ee3\u7801&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\nusing namespace std;<\/p>\n<p>template&lt;class T&gt;<br \/>\nclass shared_ptr<br \/>\n{<br \/>\npublic:<br \/>\n    \/\/\u6784\u9020&#xff1a;\u63a5\u7ba1\u88f8\u6307\u9488&#xff0c;\u521b\u5efa\u5f15\u7528\u8ba1\u6570\u3001\u4e92\u65a5\u9501<br \/>\n    explicit shared_ptr(T* p &#061; nullptr)<br \/>\n        : ptr(p)<br \/>\n    {<br \/>\n        if(ptr !&#061; nullptr)<br \/>\n        {<br \/>\n            ptrcount &#061; new int(1);<br \/>\n            mt &#061; new mutex;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            ptrcount &#061; nullptr;<br \/>\n            mt &#061; nullptr;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/\u589e\u52a0\u5f15\u7528\u8ba1\u6570<br \/>\n    void AddCount()<br \/>\n    {<br \/>\n        if(ptrcount &#061;&#061; nullptr)<br \/>\n            return;<br \/>\n        mt-&gt;lock();<br \/>\n        (*ptrcount)&#043;&#043;;<br \/>\n        mt-&gt;unlock();<br \/>\n    }<\/p>\n<p>    \/\/\u62f7\u8d1d\u6784\u9020&#xff1a;\u65b0\u5bf9\u8c61&#xff0c;\u5171\u4eabsp\u7684\u8d44\u6e90\u3001\u8ba1\u6570\u3001\u9501<br \/>\n    shared_ptr(shared_ptr&lt;T&gt;&amp; sp)<br \/>\n    {<br \/>\n        ptr &#061; sp.ptr;<br \/>\n        ptrcount &#061; sp.ptrcount;<br \/>\n        mt &#061; sp.mt;<br \/>\n        AddCount();<br \/>\n    }<\/p>\n<p>    \/\/\u6790\u6784&#xff1a;\u8c03\u7528Release\u51cf\u5c11\u8ba1\u6570&#xff0c;\u8ba1\u6570\u4e3a0\u91ca\u653e\u5168\u90e8\u8d44\u6e90<br \/>\n    ~shared_ptr()<br \/>\n    {<br \/>\n        Release();<br \/>\n    }<\/p>\n<p>    \/\/\u83b7\u53d6\u5f15\u7528\u8ba1\u6570&#xff0c;\u8fd4\u56deint&#xff0c;\u4e0d\u8fd4\u56de\u5f15\u7528&#xff0c;\u7981\u6b62\u5916\u90e8\u7be1\u6539<br \/>\n    int use_count()<br \/>\n    {<br \/>\n        if(ptrcount &#061;&#061; nullptr)<br \/>\n            return 0;<br \/>\n        mt-&gt;lock();<br \/>\n        int cnt &#061; *ptrcount;<br \/>\n        mt-&gt;unlock();<br \/>\n        return cnt;<br \/>\n    }<\/p>\n<p>    \/\/\u91ca\u653e\u5f53\u524d\u6301\u6709\u7684\u8d44\u6e90<br \/>\n    void Release()<br \/>\n    {<br \/>\n        if(ptrcount &#061;&#061; nullptr)<br \/>\n            return;<\/p>\n<p>        bool deleteflag &#061; false;<br \/>\n        mt-&gt;lock();<br \/>\n        if(&#8211;(*ptrcount) &#061;&#061; 0)<br \/>\n        {<br \/>\n            \/\/\u8ba1\u6570\u52300&#xff0c;\u91ca\u653e\u8d44\u6e90\u3001\u8ba1\u6570\u5757<br \/>\n            delete ptr;<br \/>\n            delete ptrcount;<br \/>\n            ptr &#061; nullptr;<br \/>\n            ptrcount &#061; nullptr;<br \/>\n            deleteflag &#061; true;<br \/>\n        }<br \/>\n        mt-&gt;unlock(); \/\/\u539f\u4ee3\u7801\u8fd9\u91cc\u7b14\u8bef mt\u2011lock()<\/p>\n<p>        \/\/\u53ea\u6709\u6700\u540e\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u624d\u9500\u6bc1mutex<br \/>\n        if(deleteflag)<br \/>\n        {<br \/>\n            delete mt;<br \/>\n            mt &#061; nullptr;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    T&amp; operator*() { return *ptr; }<br \/>\n    T* operator-&gt;() { return ptr; }<\/p>\n<p>private:<br \/>\n    T* ptr;         \/\/\u6258\u7ba1\u8d44\u6e90\u6307\u9488<br \/>\n    int* ptrcount;  \/\/\u5806\u4e0a\u7684\u5f15\u7528\u8ba1\u6570<br \/>\n    mutex* mt;      \/\/\u4fdd\u62a4\u5f15\u7528\u8ba1\u6570\u7684\u9501<br \/>\n};<\/p>\n<p>\u6784\u9020\u51fd\u6570\u63a5\u6536\u5916\u90e8 new \u51fa\u6765\u7684\u88f8\u6307\u9488&#xff0c;\u5728\u5806\u4e0a\u5f00\u8f9f\u5f15\u7528\u8ba1\u6570&#xff0c;\u521d\u59cb\u503c\u7f6e\u4e3a 1&#xff0c;\u540c\u65f6\u521b\u5efa\u4e00\u628a\u4e92\u65a5\u9501\u3002\u52a0\u4e0aexplicit\u5173\u952e\u5b57&#xff0c;\u9632\u6b62\u88f8\u6307\u9488\u9690\u5f0f\u8f6c\u6362\u4e3a shared_ptr \u5bf9\u8c61\u3002<\/p>\n<p>AddCount () \u8d1f\u8d23\u5f15\u7528\u8ba1\u6570\u52a0\u4e00\u3002\u4fee\u6539\u8ba1\u6570\u4e4b\u524d\u4e0a\u9501&#xff0c;\u4fee\u6539\u5b8c\u6210\u518d\u89e3\u9501&#xff0c;\u907f\u514d\u591a\u7ebf\u7a0b\u5e76\u53d1\u4fee\u6539\u628a\u8ba1\u6570\u6539\u4e71\u3002<\/p>\n<p>\u62f7\u8d1d\u6784\u9020\u4f1a\u751f\u6210\u4e00\u4e2a\u5168\u65b0\u7684 shared_ptr \u5bf9\u8c61&#xff0c;\u76f4\u63a5\u62f7\u8d1d\u6e90\u5bf9\u8c61\u7684\u8d44\u6e90\u6307\u9488\u3001\u8ba1\u6570\u6307\u9488\u3001\u9501\u6307\u9488\u3002\u591a\u4e2a\u667a\u80fd\u6307\u9488\u5171\u7528\u540c\u4e00\u5957\u8ba1\u6570\u548c\u540c\u4e00\u628a\u9501&#xff0c;\u518d\u628a\u5f15\u7528\u8ba1\u6570\u505a\u52a0 1 \u5904\u7406\u3002 \u8981\u6ce8\u610f&#xff0c;\u62f7\u8d1d\u6784\u9020\u51fa\u6765\u7684\u662f\u65b0\u5bf9\u8c61&#xff0c;\u672c\u8eab\u6ca1\u6709\u643a\u5e26\u65e7\u8d44\u6e90&#xff0c;<span style=\"color:#fe2c24\">\u4e0d\u80fd\u8c03\u7528 Release<\/span>&#xff0c;\u539f\u6765\u7684\u4ee3\u7801\u5728\u8fd9\u91cc\u903b\u8f91\u662f\u9519\u7684\u3002<\/p>\n<p>Release () \u662f\u6838\u5fc3\u91ca\u653e\u903b\u8f91&#xff0c;\u6790\u6784\u51fd\u6570\u4f1a\u8c03\u7528\u5b83\u3002\u5148\u4e0a\u9501&#xff0c;\u518d\u628a\u5f15\u7528\u8ba1\u6570\u51cf 1\u3002 \u5982\u679c\u8ba1\u6570\u51cf\u5230 0&#xff0c;\u4ee3\u8868\u5f53\u524d\u662f\u6700\u540e\u4e00\u4e2a\u4f7f\u7528\u8d44\u6e90\u7684\u5bf9\u8c61&#xff0c;\u5c31\u91ca\u653e\u6258\u7ba1\u8d44\u6e90\u4e0e\u5f15\u7528\u8ba1\u6570&#xff0c;\u628a deleteflag \u6807\u8bb0\u4e3a true&#xff1b;\u9501\u89e3\u5f00\u4e4b\u540e&#xff0c;\u518d\u53bb\u9500\u6bc1 mutex \u9501\u5bf9\u8c61\u3002 \u8981\u662f\u8ba1\u6570\u4e0d\u7b49\u4e8e 0&#xff0c;\u53ea\u505a\u8ba1\u6570\u51cf\u4e00&#xff0c;\u4e0d\u53bb\u91ca\u653e\u8d44\u6e90\u3002 \u8ba1\u6570\u5224\u65ad\u7684\u903b\u8f91\u5fc5\u987b\u653e\u5728\u9501\u7684\u4fdd\u62a4\u8303\u56f4\u5185&#xff0c;mutex \u5343\u4e07\u4e0d\u8981\u5728\u9501\u5185\u90e8 delete&#xff0c;\u8981\u89e3\u9501\u5b8c\u6210\u4e4b\u540e\u518d\u91ca\u653e\u3002<\/p>\n<p>use_count () \u7528\u6765\u62ff\u5230\u5f53\u524d\u5f15\u7528\u8ba1\u6570\u3002<span style=\"color:#fe2c24\">\u4e0d\u80fd\u8fd4\u56de int \u5f15\u7528<\/span>&#xff0c;\u4e0d\u7136\u5916\u90e8\u4ee3\u7801\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u5806\u4e0a\u7684\u5f15\u7528\u8ba1\u6570&#xff0c;\u4f1a\u628a\u667a\u80fd\u6307\u9488\u641e\u574f\u3002<\/p>\n<p>operator * \u548c operator-&gt; \u7528\u6765\u6a21\u62df\u666e\u901a\u6307\u9488\u7684\u884c\u4e3a&#xff0c;\u8ba9\u667a\u80fd\u6307\u9488\u4f7f\u7528\u8bed\u6cd5\u548c\u88f8\u6307\u9488\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<hr \/>\n<p>c.std::shared_ptr \u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/p>\n<p>shared_ptr \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u5417&#xff1f;<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5f15\u7528\u8ba1\u6570\u7684\u589e\u51cf\u6709\u52a0\u9501\u4fdd\u62a4&#xff0c;\u662f\u7ebf\u7a0b\u5b89\u5168\u7684&#xff0c;\u4f46\u5b83\u6258\u7ba1\u7684\u8d44\u6e90\u672c\u8eab\u5e76\u4e0d\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/span><\/span><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5199\u4ee3\u7801\u6d4b\u8bd5 shared_ptr \u7684\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u8981\u5206\u6e05\u4e24\u4e2a\u5173\u952e\u70b9&#xff1a;<\/p>\n<li>\u5f15\u7528\u8ba1\u6570\u662f\u6240\u6709 shared_ptr \u5171\u4eab\u7684\u3002\u5982\u679c\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u5f15\u7528\u8ba1\u6570\u505a &#043;&#043;\/&#8211;&#xff0c;\u8fd9\u4e2a\u64cd\u4f5c\u672c\u8eab\u4e0d\u662f\u539f\u5b50\u7684\u3002\u6bd4\u5982\u8ba1\u6570\u539f\u672c\u662f 1&#xff0c;\u4e24\u6b21\u5e76\u53d1 &#043;&#043;&#xff0c;\u7ed3\u679c\u6709\u53ef\u80fd\u4e0d\u662f\u9884\u671f\u7684 2&#xff0c;\u8ba1\u6570\u5c31\u4e71\u6389\u3002\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f&#xff0c;\u6216\u8005\u7a0b\u5e8f\u5d29\u6e83\u3002\u6240\u4ee5\u5f15\u7528\u8ba1\u6570\u7684\u589e\u51cf\u5fc5\u987b\u52a0\u9501\u4fdd\u62a4&#xff0c;\u4fdd\u8bc1\u8fd9\u90e8\u5206\u64cd\u4f5c\u7ebf\u7a0b\u5b89\u5168\u3002<\/li>\n<li>shared_ptr \u6258\u7ba1\u7684\u5bf9\u8c61\u5b58\u653e\u5728\u5806\u4e0a&#xff0c;\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u5199\u8fd9\u5757\u5bf9\u8c61&#xff0c;\u4f9d\u65e7\u4f1a\u4ea7\u751f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/li>\n<p>\u505a\u5b9e\u9a8c\u7684\u65f6\u5019&#xff0c;\u628a AddRefCount\u3001SubRefCount \u91cc\u9762\u7684\u9501\u53bb\u6389&#xff0c;\u5c31\u80fd\u590d\u73b0\u8ba1\u6570\u9519\u4e71\u7684 bug\u3002 \u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u5c5e\u4e8e\u5076\u73b0 bug&#xff0c;\u4e0d\u4e00\u5b9a\u6bcf\u6b21\u8fd0\u884c\u90fd\u590d\u73b0\u3002\u628a main \u91cc\u9762\u5faa\u73af\u6b21\u6570 n \u8c03\u5927&#xff0c;\u590d\u73b0\u6982\u7387\u4f1a\u660e\u663e\u63d0\u9ad8\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u7528\u81ea\u5df1\u624b\u5199\u7684 SharedPtr \u505a\u6f14\u793a&#xff0c;\u65b9\u4fbf\u89c2\u5bdf\u5f15\u7528\u8ba1\u6570\u7684\u7ebf\u7a0b\u95ee\u9898\u3002\u5982\u679c\u628a\u624b\u5199\u7248\u672c\u66ff\u6362\u6210\u6807\u51c6\u5e93\u7684 std::shared_ptr \u6d4b\u8bd5&#xff0c;\u5f97\u5230\u7684\u7ed3\u8bba\u4e5f\u662f\u4e00\u6837\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\nusing namespace std;<\/p>\n<p>struct Date<br \/>\n{<br \/>\n    int _year &#061; 0;<br \/>\n    int _month &#061; 0;<br \/>\n    int _day &#061; 0;<br \/>\n};<\/p>\n<p>void SharePtrFunc(hjq::shared_ptr&lt;Date&gt;&amp; sp, size_t n, mutex&amp; mtx)<br \/>\n{<br \/>\n    cout &lt;&lt; sp.get() &lt;&lt; endl;<\/p>\n<p>    for (size_t i &#061; 0; i &lt; n; &#043;&#043;i)<br \/>\n    {<br \/>\n        \/\/ \u8fd9\u91cc\u667a\u80fd\u6307\u9488\u62f7\u8d1d\u4f1a&#043;&#043;\u8ba1\u6570&#xff0c;\u667a\u80fd\u6307\u9488\u6790\u6784\u4f1a&#8211;\u8ba1\u6570&#xff0c;\u8fd9\u91cc\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<br \/>\n        hjq::shared_ptr&lt;Date&gt; copy(sp);<\/p>\n<p>        \/\/ \u8fd9\u91cc\u667a\u80fd\u6307\u9488\u8bbf\u95ee\u7ba1\u7406\u7684\u8d44\u6e90&#xff0c;\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u6240\u4ee5\u6211\u4eec\u770b\u770b\u8fd9\u4e9b\u503c\u4e24\u4e2a\u7ebf\u7a0b&#043;&#043;\u4e862n\u6b21&#xff0c;\u4f46\u662f\u6700\u7ec8\u770b\u5230\u7684\u7ed3\u679c&#xff0c;\u5e76\u4e00\u5b9a\u662f\u52a0\u4e862n<br \/>\n        {<br \/>\n            unique_lock&lt;mutex&gt; lk(mtx);<br \/>\n            copy-&gt;_year&#043;&#043;;<br \/>\n            copy-&gt;_month&#043;&#043;;<br \/>\n            copy-&gt;_day&#043;&#043;;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    hjq::shared_ptr&lt;Date&gt; p(new Date);<br \/>\n    cout &lt;&lt; p.get() &lt;&lt; endl;<br \/>\n    const size_t n &#061; 100000;<br \/>\n    mutex mtx;<br \/>\n    thread t1(SharePtrFunc, std::ref(p), n, std::ref(mtx));<br \/>\n    thread t2(SharePtrFunc, std::ref(p), n, std::ref(mtx));<\/p>\n<p>    t1.join();<br \/>\n    t2.join();<\/p>\n<p>    cout &lt;&lt; p-&gt;_year &lt;&lt; endl;<br \/>\n    cout &lt;&lt; p-&gt;_month &lt;&lt; endl;<br \/>\n    cout &lt;&lt; p-&gt;_day &lt;&lt; endl;<\/p>\n<p>    cout &lt;&lt; p.use_count() &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p><span style=\"color:#fe2c24\">Date \u7ed3\u6784\u4f53<\/span><\/p>\n<p>\u6d4b\u8bd5\u7528\u4e1a\u52a1\u5bf9\u8c61&#xff0c;\u5b58\u653e_year\u3001_month\u3001_day&#xff0c;\u662f shared_ptr \u6258\u7ba1\u7684\u5806\u8d44\u6e90\u3002<\/p>\n<p><span style=\"color:#fe2c24\">SharePtrFunc \u7ebf\u7a0b\u51fd\u6570<\/span><\/p>\n<p>\u63a5\u6536 shared_ptr \u5f15\u7528\u3001\u5faa\u73af\u6b21\u6570\u3001\u4e92\u65a5\u9501&#xff0c;\u6253\u5370\u8d44\u6e90\u88f8\u6307\u9488\u3002 \u5faa\u73af n \u6b21&#xff0c;\u6bcf\u6b21\u62f7\u8d1d\u6784\u9020\u5c40\u90e8copy&#xff1a; hjq::shared_ptr&lt;Date&gt; copy(sp);\u2003\u62f7\u8d1d\u6784\u9020\u5f15\u7528\u8ba1\u6570 &#043; 1&#xff1b;\u5faa\u73af\u7ed3\u675f copy \u6790\u6784&#xff0c;\u5f15\u7528\u8ba1\u6570\u20111\u3002<\/p>\n<p>\u5f15\u7528\u8ba1\u6570\u589e\u51cf\u7531 shared_ptr \u5185\u90e8\u9501\u4fdd\u969c\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5c40\u90e8\u57df\u4f7f\u7528unique_lock\u5bf9_year\/_month\/_day\u81ea\u589e\u3002 \u8fd9\u662f\u624b\u52a8\u7ed9\u4e1a\u52a1\u5bf9\u8c61\u52a0\u9501&#xff0c;\u4e0d\u662f shared_ptr \u81ea\u5e26\u9501\u3002\u53bb\u6389\u9501\u5c31\u4f1a\u53d1\u751f\u6570\u636e\u7ade\u4e89&#xff0c;\u8fd0\u7b97\u7ed3\u679c\u5c0f\u4e8e2*n<\/span><\/span>\u3002<\/p>\n<p><span style=\"color:#fe2c24\">shared_ptr<\/span> \u53ea\u8d1f\u8d23\u5f15\u7528\u8ba1\u6570&#xff0c;\u6258\u7ba1\u5bf9\u8c61\u7684\u8bfb\u5199\u5e76\u53d1&#xff0c;\u9700\u8981\u4f7f\u7528\u8005\u81ea\u5df1\u4fdd\u8bc1\u3002<\/p>\n<p><span style=\"color:#fe2c24\">main \u4e3b\u51fd\u6570<\/span><\/p>\n<p>\u521b\u5efa shared_ptr \u7ba1\u7406 new \u51fa\u6765\u7684 Date\u3002 \u542f\u52a8 t1\u3001t2 \u4e24\u4e2a\u7ebf\u7a0b&#xff0c;\u4f20\u5165\u540c\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u3002 n&#061;100000&#xff0c;\u5faa\u73af\u6570\u503c\u8d8a\u5927&#xff0c;\u5e76\u53d1\u95ee\u9898\u8d8a\u5bb9\u6613\u590d\u73b0\u3002 \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f&#xff0c;\u6253\u5370\u5e74\u6708\u65e5\u4e0e\u5f15\u7528\u8ba1\u6570use_count()\u3002<\/p>\n<hr \/>\n<p>d.std::shared_ptr \u7684\u5faa\u73af\u5f15\u7528<\/p>\n<p>shared_ptr\u867d\u7136\u4f7f\u7528\u65b9\u4fbf&#xff0c;\u4f46\u5b58\u5728\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\u3002<\/p>\n<p>\u4e3e\u4e00\u4e2a\u53cc\u5411\u94fe\u8868\u7684\u573a\u666f&#xff1a;\u94fe\u8868\u8282\u70b9\u5185\u90e8_pre\u524d\u9a71\u3001_next\u540e\u7ee7&#xff0c;\u5982\u679c\u90fd\u7528shared_ptr\u6765\u7ba1\u7406\u3002 \u5982\u679c\u53ea\u7528\u666e\u901a\u88f8\u6307\u9488&#xff0c;\u53c8\u65e0\u6cd5\u76f4\u63a5\u8d4b\u503c\u7ed9shared_ptr\u7c7b\u578b\u7684\u6210\u5458\u3002<\/p>\n<p>struct ListNode<br \/>\n{<br \/>\n    ListNode* _pre;<br \/>\n    ListNode* _next;<br \/>\n};<\/p>\n<p>int main()<br \/>\n{<br \/>\n    shared_ptr&lt;ListNode&gt; node1;<br \/>\n    shared_ptr&lt;ListNode&gt; node2;<br \/>\n    node1-&gt;_next &#061; node2;  \/\/\u9519\u8bef<br \/>\n    node2-&gt;_pre &#061; node1;   \/\/\u9519\u8bef<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u62a5\u9519\u539f\u56e0&#xff1a;_next\u3001_pre\u662f\u88f8\u6307\u9488ListNode*&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u63a5\u6536shared_ptr&lt;ListNode&gt;\u5bf9\u8c61&#xff0c;\u7c7b\u578b\u4e0d\u5339\u914d\u3002<\/p>\n<p>struct ListNode<br \/>\n{<br \/>\n    shared_ptr&lt;ListNode&gt; _pre;<br \/>\n    shared_ptr&lt;ListNode&gt; _next;<br \/>\n};<\/p>\n<p>int main()<br \/>\n{<br \/>\n    shared_ptr&lt;ListNode&gt; node1;<br \/>\n    shared_ptr&lt;ListNode&gt; node2;<br \/>\n    node1-&gt;_next &#061; node2; \/\/\u6b63\u786e<br \/>\n    node2-&gt;_pre &#061; node1;  \/\/\u6b63\u786e<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u628a\u6210\u5458\u4e5f\u5b9a\u4e49\u6210shared_ptr&#xff0c;\u7c7b\u578b\u5339\u914d&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\u3002 \u4f46\u662f\u8fd9\u91cc\u4f1a\u4ea7\u751f\u5faa\u73af\u5f15\u7528\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898&#xff1a;node1\u3001node2 \u4e92\u76f8\u6301\u6709\u5bf9\u65b9\u7684 shared_ptr&#xff0c;\u53cc\u65b9\u5f15\u7528\u8ba1\u6570\u90fd\u65e0\u6cd5\u964d\u5230 0&#xff0c;\u8282\u70b9\u4e0d\u4f1a\u91ca\u653e<\/p>\n<hr \/>\n<p>\u5f53\u5176\u4e2d\u4e24\u4e2a\u8282\u70b9\u4e92\u76f8\u5f15\u7528\u7684\u65f6\u5019&#xff0c;\u5c31\u4f1a\u51fa\u73b0\u5faa\u73af\u5f15\u7528\u7684\u73b0\u8c61\u3002\u5982\u4e0b\u6240\u793a&#xff1a;<\/p>\n<p>struct ListNode<br \/>\n{<br \/>\n    int _data;<br \/>\n    shared_ptr&lt;ListNode&gt; _pre;<br \/>\n    shared_ptr&lt;ListNode&gt; _next;<br \/>\n    ~ListNode() { cout &lt;&lt; &#034;~ListNode()&#034; &lt;&lt; endl; }<br \/>\n};<\/p>\n<p>int main()<br \/>\n{<br \/>\n    shared_ptr&lt;ListNode&gt; node1(new ListNode);<br \/>\n    shared_ptr&lt;ListNode&gt; node2(new ListNode);<br \/>\n    cout &lt;&lt; node1.use_count() &lt;&lt; endl;<br \/>\n    cout &lt;&lt; node2.use_count() &lt;&lt; endl;<\/p>\n<p>    node1-&gt;_next &#061; node2;<br \/>\n    node2-&gt;_pre &#061; node1;<br \/>\n    cout &lt;&lt; node1.use_count() &lt;&lt; endl;<br \/>\n    cout &lt;&lt; node2.use_count() &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"175\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193343-6a9b1d1770884.png\" width=\"475\" \/><\/p>\n<p><span style=\"color:#fe2c24\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"605\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193343-6a9b1d177dbdd.png\" width=\"1005\" \/><\/span><\/p>\n<p><span style=\"color:#fe2c24\">use_count&#xff08;&#xff09;: \u8fd4\u56de\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"411\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193343-6a9b1d17a7652.png\" width=\"735\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"261\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193343-6a9b1d17bb3e2.png\" width=\"735\" \/><\/p>\n<p>\u5faa\u73af\u5f15\u7528\u5206\u6790<\/p>\n<p>1.node1\u3001node2\u4e24\u4e2ashared_ptr\u5206\u522b\u7ba1\u7406\u4e24\u4e2a\u94fe\u8868\u8282\u70b9&#xff0c;\u6bcf\u4e2a\u8282\u70b9\u5f15\u7528\u8ba1\u6570\u521d\u59cb\u4e3a 1&#xff0c;\u4e0d\u7528\u624b\u52a8delete\u3002<\/p>\n<p>2.node1-&gt;_next &#061; node2&#xff0c;node2-&gt;_pre &#061; node1\u4e92\u76f8\u6307\u5411\u5bf9\u65b9&#xff0c;\u4e24\u4e2a\u8282\u70b9\u5f15\u7528\u8ba1\u6570\u90fd\u53d8\u6210 2\u3002<\/p>\n<p>3.\u51fd\u6570\u6267\u884c\u7ed3\u675f&#xff0c;\u6808\u4e0anode1\u3001node2\u5c40\u90e8\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u6790\u6784&#xff0c;\u4e24\u4e2a\u8282\u70b9\u5f15\u7528\u8ba1\u6570\u5404\u51cf 1&#xff0c;\u6700\u540e\u90fd\u5269\u4f59 1\u3002<\/p>\n<p>4.\u8282\u70b9\u5185\u90e8\u7684_next\u3001_pre\u4f9d\u65e7\u662f\u6709\u6548\u7684shared_ptr&#xff0c;\u8fd8\u4e92\u76f8\u63e1\u7740\u5bf9\u65b9\u8282\u70b9\u3002<\/p>\n<p>5.\u903b\u8f91\u95ed\u73af&#xff1a;<\/p>\n<ul>\n<li>node1\u8981\u91ca\u653e&#xff0c;\u5f97\u7b49node2\u5185\u90e8_pre\u6790\u6784&#xff1b;\u800c_pre\u662f node2 \u7684\u6210\u5458&#xff0c;\u53ea\u6709 node2 \u91ca\u653e&#xff0c;_pre\u624d\u4f1a\u6790\u6784\u3002<\/li>\n<li>node2\u8981\u91ca\u653e&#xff0c;\u5f97\u7b49node1\u5185\u90e8_next\u6790\u6784&#xff1b;\u800c_next\u662f node1 \u7684\u6210\u5458&#xff0c;\u53ea\u6709 node1 \u91ca\u653e&#xff0c;_next\u624d\u4f1a\u6790\u6784\u3002<\/li>\n<\/ul>\n<p>6.\u53cc\u65b9\u4e92\u76f8\u7b49\u5f85\u5bf9\u65b9\u5148\u91ca\u653e&#xff0c;\u8c01\u90fd\u91ca\u653e\u4e0d\u6389&#xff0c;\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f&#xff0c;\u8fd9\u5c31\u662f\u5faa\u73af\u5f15\u7528\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u53cc\u5411\u94fe\u8868&#xff0c;\u628a\u5176\u4e2d\u4e00\u4e2a\u6210\u5458\u6539\u6210weak_ptr&#xff0c;\u4e0d\u589e\u52a0\u5f15\u7528\u8ba1\u6570&#xff0c;\u6253\u7834\u5faa\u73af<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u539f\u7406&#xff1a;node1-&gt;_next &#061; node2; \u548c node2-&gt;_prev &#061; node1; \u65f6&#xff0c;weak_ptr \u7684 _next \u548c _pre \u4e0d\u4f1a\u589e\u52a0 node1 \u548c node2 \u7684\u5f15\u7528\u8ba1\u6570\u3002<\/span><\/span><\/p>\n<p>struct ListNode<br \/>\n{<br \/>\n    int _data;<br \/>\n    weak_ptr&lt;ListNode&gt; _pre;<br \/>\n    weak_ptr&lt;ListNode&gt; _next;<br \/>\n    ~ListNode(){ cout &lt;&lt; &#034;~ListNode()&#034; &lt;&lt; endl; }<br \/>\n};<\/p>\n<p>int main()<br \/>\n{<br \/>\n    shared_ptr&lt;ListNode&gt; node1(new ListNode);<br \/>\n    shared_ptr&lt;ListNode&gt; node2(new ListNode);<br \/>\n    cout &lt;&lt; node1.use_count() &lt;&lt; endl;<br \/>\n    cout &lt;&lt; node2.use_count() &lt;&lt; endl;<\/p>\n<p>    node1-&gt;_next &#061; node2;<br \/>\n    node2-&gt;_pre &#061; node1;<\/p>\n<p>    cout &lt;&lt; node1.use_count() &lt;&lt; endl;<br \/>\n    cout &lt;&lt; node2.use_count() &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<hr \/>\n<p>\u5982\u679c\u4e0d\u662f new \u51fa\u6765\u7684\u5bf9\u8c61&#xff0c;\u600e\u4e48\u7528\u667a\u80fd\u6307\u9488\u7ba1\u7406&#xff1f;<\/p>\n<p>shared_ptr\u63d0\u4f9b\u4e86\u5220\u9664\u5668\u6765\u5904\u7406\u8fd9\u7c7b\u573a\u666f&#xff0c;\u4e86\u89e3\u5373\u53ef\u3002<\/p>\n<p>\u91ca\u653e\u6570\u7ec4\u6307\u9488\u65f6&#xff0c;delete[]\u7684\u65b9\u62ec\u53f7\u4e0d\u80fd\u7701\u7565\u3002\u65b9\u62ec\u53f7\u544a\u8bc9\u7f16\u8bd1\u5668&#xff0c;\u5f53\u524d\u6307\u9488\u6307\u5411\u7684\u662f\u5bf9\u8c61\u6570\u7ec4\u9996\u5143\u7d20\u3002 \u5982\u679c\u64cd\u4f5c\u6570\u7ec4\u6307\u9488\u65f6\u6f0f\u6389[]&#xff0c;\u7a0b\u5e8f\u7f16\u8bd1\u4e0d\u4e00\u5b9a\u62a5\u9519&#xff0c;\u8fd0\u884c\u65f6\u4f1a\u76f4\u63a5\u51fa\u73b0\u672a\u5b9a\u4e49\u884c\u4e3a\u3002<\/p>\n<p>int* ptr &#061; new int[5];<br \/>\ndelete[] ptr;<br \/>\n\/\/delete ptr; \/\/error <\/p>\n<p>\u6211\u4eec\u5982\u679c\u5728\u52a8\u6001\u5185\u5b58\u4e2d\u521b\u5efa\u51fa\u4e00\u4e2a\u6570\u7ec4&#xff0c;\u7528\u4e00\u4e2a shared_ptr \u5bf9\u8c61\u53bb\u6307\u5411\u8be5\u6570\u7ec4&#xff0c;\u5f53 shared_ptr \u4f7f\u7528\u5b8c\u540e&#xff0c;\u5c31\u4f1a\u53bb\u8c03\u7528\u6790\u6784\u51fd\u6570&#xff0c;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u7531\u4e8e shared_ptr \u9ed8\u8ba4\u7684\u5220\u9664\u65b9\u5f0f\u662f delete ptr&#xff0c;\u540e\u9762\u6ca1\u6709\u5e26\u65b9\u62ec\u53f7<\/span><\/span>&#xff0c;\u90a3\u4e48\u7a0b\u5e8f\u5c31\u4f1a\u5d29\u6389\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"695\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193343-6a9b1d17cce05.png\" width=\"1095\" \/><\/p>\n<p>\u5982\u679c\u6253\u5f00\u6587\u4ef6\u5f97\u5230\u6587\u4ef6\u6307\u9488&#xff0c;\u4ea4\u7ed9shared_ptr\u7ba1\u7406\u3002\u667a\u80fd\u6307\u9488\u6790\u6784\u65f6\u4e0d\u80fd\u76f4\u63a5\u7528delete\u91ca\u653e&#xff0c;\u9700\u8981\u8c03\u7528close()\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<p>\u6240\u4ee5shared_ptr\u63d0\u4f9b\u4e86\u652f\u6301\u81ea\u5b9a\u4e49\u5220\u9664\u5668\u7684\u6784\u9020\u51fd\u6570&#xff0c;\u7528\u6765\u6307\u5b9a\u8d44\u6e90\u7684\u91ca\u653e\u903b\u8f91\u3002<\/p>\n<p> template &lt;class U, class D&gt; shared_ptr (U* p, D del);<\/p>\n<p>\u81ea\u5b9a\u4e49\u5220\u9664\u5668\u53ef\u4ee5\u662f&#xff1a;\u51fd\u6570\u6307\u9488\u3001\u4eff\u51fd\u6570\u3001lambda\u3001\u5305\u88c5\u5668\u3002<\/p>\n<p>\u3010\u4eff\u51fd\u6570\u7684\u5220\u9664\u5668\u3011<\/p>\n<p>shared_ptr \u4e2d\u7684\u6790\u6784\u51fd\u6570\u4f1a\u53bb\u8c03\u7528 DelArry \u4eff\u51fd\u6570\u53bb\u91ca\u653e\u52a8\u6001\u6570\u7ec4\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>struct ListNode<br \/>\n{<br \/>\n    std::shared_ptr&lt;ListNode&gt; _pre;<br \/>\n    std::shared_ptr&lt;ListNode&gt; _next;<br \/>\n};<\/p>\n<p>template&lt;class T&gt;<br \/>\nclass DelArray<br \/>\n{<br \/>\npublic:<br \/>\n    void operator()(T* array)<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;delete []&#034; &lt;&lt; std::endl;<br \/>\n        delete[] array;<br \/>\n    }<br \/>\n};<\/p>\n<p>int main()<br \/>\n{<br \/>\n    \/\/ new[]\u5f00\u8f9f\u6570\u7ec4&#xff0c;\u4f20\u5165\u4eff\u51fd\u6570\u5bf9\u8c61\u4f5c\u4e3a\u81ea\u5b9a\u4e49\u5220\u9664\u5668<br \/>\n    std::shared_ptr&lt;ListNode&gt; ptr1(new ListNode[10], DelArray&lt;ListNode&gt;());<br \/>\n    return 0;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"80\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193344-6a9b1d1825d7c.png\" width=\"521\" \/><\/p>\n<hr \/>\n<p>e.std::shared_ptr \u548c std::weak_ptr \u7684\u6a21\u62df\u5b9e\u73b0<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\nusing namespace std;<\/p>\n<p>namespace hjq<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            , _pRefCount(new int(1))<br \/>\n            , _pmtx(new mutex)<br \/>\n        {}<\/p>\n<p>        \/\/\u62f7\u8d1d\u6784\u9020&#xff1a;\u5171\u4eab\u8d44\u6e90\u3001\u8ba1\u6570\u3001\u9501&#xff0c;\u8ba1\u6570&#043;1<br \/>\n        shared_ptr(const shared_ptr&lt;T&gt;&amp; sp)<br \/>\n            : _ptr(sp._ptr)<br \/>\n            , _pRefCount(sp._pRefCount)<br \/>\n            , _pmtx(sp._pmtx)<br \/>\n        {<br \/>\n            AddRef();<br \/>\n        }<\/p>\n<p>        void Release()<br \/>\n        {<br \/>\n            _pmtx-&gt;lock();<br \/>\n            bool flag &#061; false;<br \/>\n            \/\/\u5f15\u7528\u8ba1\u6570\u51cf\u52300&#xff0c;\u624d\u91ca\u653e\u8d44\u6e90<br \/>\n            if (&#8211;(*_pRefCount) &#061;&#061; 0 &amp;&amp; _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                delete _pRefCount;<br \/>\n                flag &#061; true;<br \/>\n            }<br \/>\n            _pmtx-&gt;unlock();<br \/>\n            \/\/\u4e0d\u80fd\u5728lock\u5185\u90e8\u9500\u6bc1mutex&#xff0c;\u89e3\u9501\u540e\u518d\u5220<br \/>\n            if (flag)<br \/>\n            {<br \/>\n                delete _pmtx;<br \/>\n            }<br \/>\n        }<\/p>\n<p>        void AddRef()<br \/>\n        {<br \/>\n            _pmtx-&gt;lock();<br \/>\n            &#043;&#043;(*_pRefCount);<br \/>\n            _pmtx-&gt;unlock();<br \/>\n        }<\/p>\n<p>        \/\/\u62f7\u8d1d\u8d4b\u503c<br \/>\n        shared_ptr&lt;T&gt;&amp; operator&#061;(const shared_ptr&lt;T&gt;&amp; sp)<br \/>\n        {<br \/>\n            if (this !&#061; &amp;sp)<br \/>\n            {<br \/>\n                Release();<br \/>\n                _ptr &#061; sp._ptr;<br \/>\n                _pRefCount &#061; sp._pRefCount;<br \/>\n                _pmtx &#061; sp._pmtx;<br \/>\n                AddRef();<br \/>\n            }<br \/>\n            return *this;<br \/>\n        }<\/p>\n<p>        int use_count() const<br \/>\n        {<br \/>\n            return *_pRefCount;<br \/>\n        }<\/p>\n<p>        ~shared_ptr()<br \/>\n        {<br \/>\n            Release();<br \/>\n        }<\/p>\n<p>        T&amp; operator*() const<br \/>\n        {<br \/>\n            return *_ptr;<br \/>\n        }<\/p>\n<p>        T* operator-&gt;() const<br \/>\n        {<br \/>\n            return _ptr;<br \/>\n        }<\/p>\n<p>        T* get() const<br \/>\n        {<br \/>\n            return _ptr;<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        T* _ptr;<br \/>\n        int* _pRefCount;<br \/>\n        mutex* _pmtx;<br \/>\n    };<\/p>\n<p>    \/\/\u7b80\u5316\u7248weak_ptr&#xff08;\u8fd9\u4e2a\u5b9e\u73b0\u6b8b\u7f3a&#xff0c;\u4ec5\u6f14\u793a\u5916\u58f3&#xff09;<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        {}<\/p>\n<p>        weak_ptr(const shared_ptr&lt;T&gt;&amp; sp)<br \/>\n            :_ptr(sp.get())<br \/>\n        {}<\/p>\n<p>        weak_ptr&lt;T&gt;&amp; operator&#061;(const shared_ptr&lt;T&gt;&amp; sp)<br \/>\n        {<br \/>\n            _ptr &#061; sp.get();<br \/>\n            return *this;<br \/>\n        }<\/p>\n<p>        T&amp; operator*() const<br \/>\n        {<br \/>\n            return *_ptr;<br \/>\n        }<\/p>\n<p>        T* operator-&gt;() const<br \/>\n        {<br \/>\n            return _ptr;<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        T* _ptr;<br \/>\n    };<br \/>\n}<\/p>\n<p>shared_ptr \u6210\u5458<\/p>\n<ul>\n<li>_ptr&#xff1a;\u4fdd\u5b58\u8981\u7ba1\u7406\u7684\u5806\u5185\u5b58\u88f8\u6307\u9488<\/li>\n<li>_pRefCount&#xff1a;\u5806\u4e0a\u7684\u5f15\u7528\u8ba1\u6570&#xff0c;\u6240\u6709\u62f7\u8d1d\u51fa\u6765\u7684\u5bf9\u8c61\u5171\u7528\u8fd9\u4e00\u4efd<\/li>\n<li>_pmtx&#xff1a;\u4e92\u65a5\u9501&#xff0c;\u4e13\u95e8\u4fdd\u62a4\u5f15\u7528\u8ba1\u6570&#xff0c;\u591a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u8ba1\u6570\u7684\u65f6\u5019\u4fdd\u8bc1\u5b89\u5168<\/li>\n<\/ul>\n<p>\u6784\u9020\u51fd\u6570 \u4f20\u5165\u88f8\u6307\u9488&#xff0c;\u5f15\u7528\u8ba1\u6570\u76f4\u63a5\u521d\u59cb\u5316\u4e3a 1&#xff0c;\u5728\u5806\u4e0a\u5f00\u51fa\u8ba1\u6570\u548c\u9501\u3002<\/p>\n<p>\u6ce8\u610f&#xff1a;\u5343\u4e07\u522b\u62ff\u540c\u4e00\u4e2a\u88f8\u6307\u9488\u53bb\u591a\u6b21\u6784\u9020 shared_ptr&#xff0c;\u4f1a\u4ea7\u751f\u591a\u5957\u72ec\u7acb\u7684\u63a7\u5236\u5757&#xff0c;\u6700\u540e\u4f1a\u91cd\u590d\u91ca\u653e\u5185\u5b58\u3002<\/p>\n<p>\u62f7\u8d1d\u6784\u9020 \u76f4\u63a5\u590d\u5236\u8d44\u6e90\u6307\u9488\u3001\u8ba1\u6570\u6307\u9488\u3001\u9501\u6307\u9488&#xff0c;\u4e0d\u4f1a\u91cd\u65b0\u5f00\u8f9f\u63a7\u5236\u5757&#xff1b;\u8c03\u7528AddRef()&#xff0c;\u5f15\u7528\u8ba1\u6570\u52a0 1\u3002<\/p>\n<p>AddRef \u5148\u4e0a\u9501&#xff0c;\u5f15\u7528\u8ba1\u6570\u81ea\u589e&#xff0c;\u518d\u89e3\u9501&#xff0c;\u7528\u6765\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u8ba1\u6570\u4e0d\u51fa\u9519\u3002<\/p>\n<p>Release&#xff08;\u6838\u5fc3\u91ca\u653e\u903b\u8f91&#xff09; \u6790\u6784\u3001\u62f7\u8d1d\u8d4b\u503c\u90fd\u4f1a\u8d70\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<ul>\n<li>\u4e0a\u9501&#xff0c;\u5f15\u7528\u8ba1\u6570\u51cf 1<\/li>\n<li>\u5982\u679c\u8ba1\u6570\u51cf\u5230 0&#xff0c;\u8bf4\u660e\u8fd9\u662f\u6700\u540e\u4e00\u4e2a\u6301\u6709\u8d44\u6e90\u7684\u5bf9\u8c61&#xff0c;\u91ca\u653e\u7ba1\u7406\u7684\u5185\u5b58&#xff0c;\u518d\u91ca\u653e\u5f15\u7528\u8ba1\u6570<\/li>\n<li>mutex \u4e0d\u80fd\u5728\u52a0\u9501\u72b6\u6001\u4e0b\u76f4\u63a5 delete&#xff0c;\u7528 flag \u505a\u6807\u8bb0&#xff0c;\u89e3\u9501\u4e4b\u540e\u518d\u9500\u6bc1\u9501\u5bf9\u8c61<\/li>\n<li>\u8ba1\u6570\u4e0d\u7b49\u4e8e 0&#xff0c;\u53ea\u505a\u8ba1\u6570\u51cf 1&#xff0c;\u4e0d\u4f1a\u91ca\u653e\u4efb\u4f55\u8d44\u6e90<\/li>\n<\/ul>\n<p>\u62f7\u8d1d\u8d4b\u503c \u5148\u8c03\u7528Release\u628a\u81ea\u5df1\u539f\u6765\u6301\u6709\u7684\u65e7\u8d44\u6e90\u5904\u7406\u5e72\u51c0&#xff0c;\u518d\u63a5\u7ba1\u5bf9\u65b9\u7684\u8d44\u6e90&#xff0c;\u7136\u540e\u5f15\u7528\u8ba1\u6570 &#043; 1\u3002<\/p>\n<p>\u6790\u6784\u51fd\u6570 \u76f4\u63a5\u8c03\u7528Release&#xff0c;\u771f\u6b63\u8981\u4e0d\u8981\u91ca\u653e\u5185\u5b58\u4ea4\u7ed9 Release \u53bb\u5224\u65ad\u3002<\/p>\n<p>operator*\u3001operator-&gt;\u3001get()&#xff1a;\u6a21\u4eff\u666e\u901a\u6307\u9488\u7684\u7528\u6cd5&#xff0c;\u8ba9\u667a\u80fd\u6307\u9488\u53ef\u4ee5\u50cf\u88f8\u6307\u9488\u4e00\u6837\u8bbf\u95ee\u8d44\u6e90\u3002<\/p>\n<p>weak_ptr&#xff08;\u6211\u5199\u7684\u8fd9\u4e2a&#xff09; \u8fd9\u4e2a\u7b80\u6613\u5b9e\u73b0\u53ea\u5b58\u4e86\u539f\u59cb\u88f8\u6307\u9488&#xff0c;\u4e0d\u4f1a\u589e\u52a0\u5f15\u7528\u8ba1\u6570\u3002 \u4f46\u662f\u6ca1\u6709\u5173\u8054\u63a7\u5236\u5757&#xff0c;\u7f3a\u5c11expired()\u3001lock()\u63a5\u53e3&#xff0c;\u6ca1\u529e\u6cd5\u5224\u65ad\u8d44\u6e90\u662f\u5426\u5df2\u7ecf\u88ab\u91ca\u653e&#xff0c;\u53ea\u662f\u4e00\u4e2a\u5916\u58f3&#xff0c;\u4e0d\u80fd\u771f\u6b63\u7528\u6765\u89e3\u51b3\u5faa\u73af\u5f15\u7528\u95ee\u9898\u3002<\/p>\n<\/p>\n<hr \/>\n<h2>\u56db.C&#043;&#043;11 \u4e0e boost \u667a\u80fd\u6307\u9488\u7684\u53d1\u5c55\u5173\u7cfb<\/h2>\n<li>C&#043;&#043;98 \u6700\u65e9\u51fa\u73b0\u4e86auto_ptr&#xff0c;\u8fd9\u662f\u7b2c\u4e00\u4e2a\u6807\u51c6\u5e93\u667a\u80fd\u6307\u9488&#xff0c;\u4f46\u62f7\u8d1d\u884c\u4e3a\u6709\u7f3a\u9677&#xff0c;\u5b9e\u9645\u4f7f\u7528\u5f88\u5bb9\u6613\u8e29\u5751\u3002<\/li>\n<li>boost \u5e93\u540e\u9762\u5b9e\u73b0\u4e86\u66f4\u5b9e\u7528\u7684\u51e0\u6b3e\u667a\u80fd\u6307\u9488&#xff1a;scoped_ptr\u3001shared_ptr\u3001weak_ptr\u3002<\/li>\n<li>\u4e4b\u540e TR1 \u6280\u672f\u62a5\u544a\u628ashared_ptr\u3001weak_ptr\u5f15\u5165\u8fdb\u6765&#xff0c;TR1 \u53ea\u662f\u6280\u672f\u6269\u5c55&#xff0c;\u4e0d\u5c5e\u4e8e\u6b63\u5f0f C&#043;&#043; \u6807\u51c6\u3002<\/li>\n<li>\u5230 C&#043;&#043;11 \u6b63\u5f0f\u6807\u51c6&#xff0c;\u628a\u667a\u80fd\u6307\u9488\u6536\u8fdb\u6807\u51c6\u5e93&#xff1a;unique_ptr\u3001shared_ptr\u3001weak_ptr\u3002unique_ptr\u5927\u4f53\u5bf9\u5e94 boost \u7684scoped_ptr&#xff0c;\u72ec\u5360\u8d44\u6e90&#xff0c;\u4e0d\u5141\u8bb8\u62f7\u8d1d&#xff1b;shared_ptr\u3001weak_ptr\u4e5f\u53c2\u8003 boost \u7684\u5b9e\u73b0\u601d\u8def\u7eb3\u5165\u6807\u51c6\u3002<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.\u4e3a\u4ec0\u4e48\u6211\u4eec\u4f1a\u9700\u8981\u667a\u80fd\u6307\u9488 \u56e0\u4e3a\u9700\u8981\u5e2e C \u7a0b\u5e8f\u5458\u7ba1\u7406\u5806\u4e0a new \u51fa\u6765\u7684\u5185\u5b58&#xff0c;\u81ea\u52a8\u5b8c\u6210\u91ca\u653e&#xff0c;\u7528\u6765\u89e3\u51b3\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002 #include<br \/>\n#include<br \/>\n#include<br \/>\nusing namespace std;\/\/ \u52a8\u6001\u5206\u914d\u5185\u5b58&#xff0c;\u6ca1\u6709\u91ca\u653e\u5c31return<br \/>\nvoid memoryLeak1()<br \/>\n{<br \/>\nstring *str  new string(\\&#8221;\u52a8\u6001\u5206\u914d\u5185\u5b58&#xff01;\\&#8221;<\/p>\n","protected":false},"author":2,"featured_media":100669,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11511,55,3752,4041,4221],"topic":[],"class_list":["post-100689","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-raii","tag-c","tag-c11","tag-4041","tag-4221"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010C++\u3011\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u53ca\u5176\u539f\u7406-\u8be6\u89e3 - \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\/100689.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010C++\u3011\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u53ca\u5176\u539f\u7406-\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00.\u4e3a\u4ec0\u4e48\u6211\u4eec\u4f1a\u9700\u8981\u667a\u80fd\u6307\u9488 \u56e0\u4e3a\u9700\u8981\u5e2e C \u7a0b\u5e8f\u5458\u7ba1\u7406\u5806\u4e0a new \u51fa\u6765\u7684\u5185\u5b58&#xff0c;\u81ea\u52a8\u5b8c\u6210\u91ca\u653e&#xff0c;\u7528\u6765\u89e3\u51b3\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002 #include #include #include using namespace std;\/\/ \u52a8\u6001\u5206\u914d\u5185\u5b58&#xff0c;\u6ca1\u6709\u91ca\u653e\u5c31return void memoryLeak1() { string *str new string(&quot;\u52a8\u6001\u5206\u914d\u5185\u5b58&#xff01;&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/100689.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-04T19:33:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193340-6a9b1d14f2d30.png\" \/>\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=\"18 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/100689.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/100689.html\",\"name\":\"\u3010C++\u3011\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u53ca\u5176\u539f\u7406-\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-04T19:33:45+00:00\",\"dateModified\":\"2026-09-04T19:33:45+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/100689.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/100689.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/100689.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010C++\u3011\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u53ca\u5176\u539f\u7406--\u8be6\u89e3\"}]},{\"@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++\u3011\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u53ca\u5176\u539f\u7406-\u8be6\u89e3 - \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\/100689.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010C++\u3011\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u53ca\u5176\u539f\u7406-\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00.\u4e3a\u4ec0\u4e48\u6211\u4eec\u4f1a\u9700\u8981\u667a\u80fd\u6307\u9488 \u56e0\u4e3a\u9700\u8981\u5e2e C \u7a0b\u5e8f\u5458\u7ba1\u7406\u5806\u4e0a new \u51fa\u6765\u7684\u5185\u5b58&#xff0c;\u81ea\u52a8\u5b8c\u6210\u91ca\u653e&#xff0c;\u7528\u6765\u89e3\u51b3\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002 #include #include #include using namespace std;\/\/ \u52a8\u6001\u5206\u914d\u5185\u5b58&#xff0c;\u6ca1\u6709\u91ca\u653e\u5c31return void memoryLeak1() { string *str new string(\"\u52a8\u6001\u5206\u914d\u5185\u5b58&#xff01;\"","og_url":"https:\/\/www.wsisp.com\/helps\/100689.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-04T19:33:45+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904193340-6a9b1d14f2d30.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"18 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/100689.html","url":"https:\/\/www.wsisp.com\/helps\/100689.html","name":"\u3010C++\u3011\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u53ca\u5176\u539f\u7406-\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-04T19:33:45+00:00","dateModified":"2026-09-04T19:33:45+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/100689.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/100689.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/100689.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010C++\u3011\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u53ca\u5176\u539f\u7406--\u8be6\u89e3"}]},{"@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\/100689","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=100689"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/100689\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/100669"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=100689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=100689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=100689"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=100689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}