{"id":74004,"date":"2026-02-08T22:33:00","date_gmt":"2026-02-08T14:33:00","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/74004.html"},"modified":"2026-02-08T22:33:00","modified_gmt":"2026-02-08T14:33:00","slug":"%e3%80%90c%e3%80%91-%e6%9e%90%e6%9e%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/74004.html","title":{"rendered":"\u3010C++\u3011 \u6790\u6784\u51fd\u6570"},"content":{"rendered":"<h2>\u6790\u6784\u51fd\u6570\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<h3>\u4ec0\u4e48\u662f\u6790\u6784\u51fd\u6570<\/h3>\n<p>\u6790\u6784\u51fd\u6570&#xff08;destructor&#xff09;\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6210\u5458\u51fd\u6570&#xff0c;\u4e0e\u6784\u9020\u51fd\u6570\u529f\u80fd\u76f8\u53cd\u3002\u5f53\u5bf9\u8c61\u7ed3\u675f\u5176\u751f\u547d\u5468\u671f\u65f6&#xff08;\u5982\u5bf9\u8c61\u6240\u5728\u7684\u51fd\u6570\u5df2\u8c03\u7528\u5b8c\u6bd5&#xff09;&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u6267\u884c\u6790\u6784\u51fd\u6570\u3002\u6790\u6784\u51fd\u6570\u4e3b\u8981\u5b8c\u6210&#034;\u6e05\u7406\u5584\u540e&#034;\u5de5\u4f5c&#xff0c;\u4f8b\u5982\u91ca\u653e\u5bf9\u8c61\u5728\u751f\u547d\u5468\u671f\u4e2d\u7533\u8bf7\u7684\u8d44\u6e90\u3002<\/p>\n<h3>\u6790\u6784\u51fd\u6570\u7684\u58f0\u660e\u4e0e\u5b9a\u4e49<\/h3>\n<p>\u5728C&#043;&#043;\u4e2d&#xff0c;\u6790\u6784\u51fd\u6570\u7684\u58f0\u660e\u548c\u5b9a\u4e49\u9075\u5faa\u7279\u5b9a\u8bed\u6cd5\u89c4\u5219&#xff1a;<\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ClassName<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token operator\">~<\/span><span class=\"token function\">ClassName<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u6790\u6784\u51fd\u6570\u58f0\u660e<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token class-name\">ClassName<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token operator\">~<\/span><span class=\"token function\">ClassName<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6790\u6784\u51fd\u6570\u5b9a\u4e49&#xff08;\u51fd\u6570\u4f53&#xff09;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u6790\u6784\u51fd\u6570\u7684\u540d\u79f0\u4e3a\u7c7b\u540d\u524d\u52a0\u6ce2\u6d6a\u7b26&#xff08;~&#xff09;&#xff0c;\u6ca1\u6709\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;\u4e5f\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\u3002<\/p>\n<h2>\u6790\u6784\u51fd\u6570\u7684\u7279\u6027\u4e0e\u5de5\u4f5c\u539f\u7406<\/h2>\n<h3>\u6838\u5fc3\u7279\u6027<\/h3>\n<p>\u6790\u6784\u51fd\u6570\u5177\u6709\u4ee5\u4e0b\u5173\u952e\u7279\u6027&#xff1a;<\/p>\n<ul>\n<li>\u4e0e\u7c7b\u540d\u76f8\u540c\u200b\u200b&#xff1a;\u5728\u540d\u79f0\u524d\u52a0\u6ce2\u6d6a\u7b26&#xff08;~&#xff09;\u4ee5\u533a\u522b\u4e8e\u6784\u9020\u51fd\u6570<\/li>\n<li>\u200b\u200b\u65e0\u53c2\u6570\u65e0\u8fd4\u56de\u503c\u200b\u200b&#xff1a;\u4e0d\u80fd\u5e26\u4efb\u4f55\u53c2\u6570&#xff0c;\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c&#xff08;\u5305\u62ecvoid\u7c7b\u578b&#xff09;<\/li>\n<li>\u200b\u200b\u4e0d\u53ef\u91cd\u8f7d\u200b\u200b&#xff1a;\u6bcf\u4e2a\u7c7b\u53ea\u80fd\u6709\u4e00\u4e2a\u6790\u6784\u51fd\u6570<\/li>\n<li>\u200b\u200b\u81ea\u52a8\u8c03\u7528\u200b\u200b&#xff1a;\u7531\u7cfb\u7edf\u5728\u5bf9\u8c61\u9500\u6bc1\u65f6\u81ea\u52a8\u6267\u884c&#xff0c;\u4e0d\u80fd\u624b\u52a8\u8c03\u7528<\/li>\n<\/ul>\n<h3>\u9ed8\u8ba4\u6790\u6784\u51fd\u6570<\/h3>\n<p>\u5982\u679c\u7528\u6237\u6ca1\u6709\u663e\u5f0f\u5b9a\u4e49\u6790\u6784\u51fd\u6570&#xff0c;\u7f16\u8bd1\u7cfb\u7edf\u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u7f3a\u7701\u7684\u6790\u6784\u51fd\u6570\u3002\u8fd9\u4e2a\u9ed8\u8ba4\u6790\u6784\u51fd\u6570\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c&#xff0c;\u4ec5\u63d0\u4f9b\u57fa\u672c\u7684\u5bf9\u8c61\u9500\u6bc1\u673a\u5236\u3002<\/p>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f&#xff0c;\u5373\u4f7f\u81ea\u5b9a\u4e49\u4e86\u6790\u6784\u51fd\u6570&#xff0c;\u7f16\u8bd1\u5668\u4e5f\u603b\u662f\u4f1a\u4e3a\u6211\u4eec\u5408\u6210\u4e00\u4e2a\u6790\u6784\u51fd\u6570\u3002\u5982\u679c\u81ea\u5b9a\u4e49\u4e86\u6790\u6784\u51fd\u6570&#xff0c;\u7f16\u8bd1\u5668\u5728\u6267\u884c\u65f6\u4f1a\u5148\u8c03\u7528\u81ea\u5b9a\u4e49\u7684\u6790\u6784\u51fd\u6570&#xff0c;\u518d\u8c03\u7528\u5408\u6210\u7684\u6790\u6784\u51fd\u6570\u3002<\/p>\n<h3>\u6790\u6784\u51fd\u6570\u7684\u8c03\u7528\u65f6\u673a<\/h3>\n<p>\u6790\u6784\u51fd\u6570\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528&#xff1a;<\/p>\n<li>\u200b\u200b\u5c40\u90e8\u5bf9\u8c61\u200b\u200b&#xff1a;\u5f53\u51fd\u6570\u6267\u884c\u7ed3\u675f&#xff0c;\u5c40\u90e8\u5bf9\u8c61\u79bb\u5f00\u5176\u4f5c\u7528\u57df\u65f6<\/li>\n<li>\u200b\u200b\u5168\u5c40\u5bf9\u8c61\u200b\u200b&#xff1a;\u7a0b\u5e8f\u7ed3\u675f\u65f6&#xff0c;\u5168\u5c40\u5bf9\u8c61\u548c\u9759\u6001\u5bf9\u8c61\u7684\u6790\u6784\u51fd\u6570\u88ab\u8c03\u7528<\/li>\n<li>\u200b\u200b\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\u200b\u200b&#xff1a;\u4f7f\u7528delete\u8fd0\u7b97\u7b26\u91ca\u653e\u5bf9\u8c61\u65f6<\/li>\n<li>\u200b\u200b\u4e34\u65f6\u5bf9\u8c61\u200b\u200b&#xff1a;\u4e34\u65f6\u5bf9\u8c61\u5b8c\u6210\u5176\u4f7f\u547d\u540e<\/li>\n<h2>\u6790\u6784\u51fd\u6570\u7684\u5b9e\u9645\u5e94\u7528<\/h2>\n<h3>\u8d44\u6e90\u7ba1\u7406<\/h3>\n<p>\u6790\u6784\u51fd\u6570\u6700\u5e38\u89c1\u7684\u7528\u9014\u662f\u91ca\u653e\u5bf9\u8c61\u5728\u751f\u547d\u5468\u671f\u4e2d\u7533\u8bf7\u7684\u8d44\u6e90&#xff0c;\u5982\u52a8\u6001\u5185\u5b58\u3001\u6587\u4ef6\u53e5\u67c4\u3001\u7f51\u7edc\u8fde\u63a5\u7b49\u3002<br \/>\n\u793a\u4f8b<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;cstring&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Student<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u9ed8\u8ba4\u6784\u9020\u51fd\u6570<\/span><br \/>\n    <span class=\"token function\">Student<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>name<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> age<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u52a8\u6001\u5206\u914d\u5185\u5b58\u5e76\u590d\u5236\u5b57\u7b26\u4e32<\/span><br \/>\n        <span class=\"token keyword\">this<\/span><span class=\"token operator\">-&gt;<\/span>name <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token keyword\">char<\/span><span class=\"token punctuation\">[<\/span><span class=\"token function\">strlen<\/span><span class=\"token punctuation\">(<\/span>name<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">strcpy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">this<\/span><span class=\"token operator\">-&gt;<\/span>name<span class=\"token punctuation\">,<\/span> name<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">this<\/span><span class=\"token operator\">-&gt;<\/span>age <span class=\"token operator\">&#061;<\/span> age<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u6790\u6784\u51fd\u6570<\/span><br \/>\n    <span class=\"token operator\">~<\/span><span class=\"token function\">Student<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token double-colon punctuation\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Destructor called for: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> name <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token double-colon punctuation\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">delete<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> name<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u91ca\u653e\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u663e\u793a\u5b66\u751f\u4fe1\u606f<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">display<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token double-colon punctuation\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Name: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> name <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;, Age: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> age <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token double-colon punctuation\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>name<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u52a8\u6001\u5206\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> age<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        Student <span class=\"token function\">student1<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Alice&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61<\/span><br \/>\n        student1<span class=\"token punctuation\">.<\/span><span class=\"token function\">display<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>            <span class=\"token comment\">\/\/ \u8f93\u51fa: Name: Alice, Age: 20<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token comment\">\/\/ student1\u7684\u4f5c\u7528\u57df\u7ed3\u675f&#xff0c;\u6790\u6784\u51fd\u6570\u4f1a\u88ab\u8c03\u7528<\/span><\/p>\n<p>    <span class=\"token punctuation\">{<\/span><br \/>\n        Student <span class=\"token function\">student2<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Bob&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">22<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u521b\u5efa\u53e6\u4e00\u4e2a\u5bf9\u8c61<\/span><br \/>\n        student2<span class=\"token punctuation\">.<\/span><span class=\"token function\">display<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>          <span class=\"token comment\">\/\/ \u8f93\u51fa: Name: Bob, Age: 22<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token comment\">\/\/ student2\u7684\u4f5c\u7528\u57df\u7ed3\u675f&#xff0c;\u6790\u6784\u51fd\u6570\u4f1a\u88ab\u8c03\u7528<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u8fd9\u79cd\u8d44\u6e90\u7ba1\u7406\u65b9\u5f0f\u662fC&#043;&#043;\u4e2dRAII&#xff08;Resource Acquisition Is Initialization&#xff09;\u7406\u5ff5\u7684\u6838\u5fc3\u5b9e\u8df5&#xff0c;\u786e\u4fdd\u8d44\u6e90\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u88ab\u6b63\u786e\u91ca\u653e\u3002<\/p>\n<h3>\u6790\u6784\u987a\u5e8f<\/h3>\n<p>\u591a\u4e2a\u5bf9\u8c61\u7684\u6790\u6784\u51fd\u6570\u8c03\u7528\u987a\u5e8f\u4e0e\u6784\u9020\u51fd\u6570\u8c03\u7528\u987a\u5e8f\u76f8\u53cd&#xff1a;\u200b\u200b\u6700\u5148\u6784\u9020\u7684\u5bf9\u8c61\u6700\u540e\u88ab\u6790\u6784\u200b\u200b&#xff0c;\u6700\u540e\u6784\u9020\u7684\u5bf9\u8c61\u6700\u5148\u88ab\u6790\u6784\u3002<\/p>\n<p>\u5177\u4f53\u6765\u8bf4&#xff1a;<\/p>\n<ul>\n<li>\u200b\u200b\u5168\u5c40\u5bf9\u8c61\u200b\u200b&#xff1a;\u5728\u6240\u6709\u51fd\u6570&#xff08;\u5305\u62ecmain\u51fd\u6570&#xff09;\u6267\u884c\u4e4b\u524d\u6784\u9020&#xff0c;\u5728main\u51fd\u6570\u7ed3\u675f\u6216\u8c03\u7528exit\u51fd\u6570\u65f6\u6790\u6784<\/li>\n<li>\u200b\u200b\u5c40\u90e8\u81ea\u52a8\u5bf9\u8c61\u200b\u200b&#xff1a;\u5728\u5efa\u7acb\u5bf9\u8c61\u65f6\u8c03\u7528\u6784\u9020\u51fd\u6570&#xff0c;\u51fd\u6570\u8c03\u7528\u7ed3\u675f\u65f6\u8c03\u7528\u6790\u6784\u51fd\u6570<\/li>\n<li>\u200b\u200b\u9759\u6001\u5c40\u90e8\u5bf9\u8c61\u200b\u200b&#xff1a;\u5728\u7a0b\u5e8f\u7b2c\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u5efa\u7acb\u5bf9\u8c61\u65f6\u8c03\u7528\u6784\u9020\u51fd\u6570&#xff0c;\u5728main\u51fd\u6570\u7ed3\u675f\u6216\u8c03\u7528exit\u51fd\u6570\u65f6\u6790\u6784<\/li>\n<\/ul>\n<h2>\u91cd\u8981\u6ce8\u610f\u4e8b\u9879\u4e0e\u6027\u80fd\u8003\u91cf<\/h2>\n<h3>\u6790\u6784\u51fd\u6570\u7684\u5f02\u5e38\u5904\u7406<\/h3>\n<p>\u6790\u6784\u51fd\u6570\u4e0d\u5e94\u629b\u51fa\u5f02\u5e38\u3002\u5982\u679c\u6790\u6784\u51fd\u6570\u4e2d\u53ef\u80fd\u53d1\u751f\u5f02\u5e38&#xff0c;\u5fc5\u987b\u5728\u6790\u6784\u51fd\u6570\u5185\u90e8\u6355\u83b7\u5e76\u5904\u7406\u5b83\u4eec&#xff0c;\u907f\u514d\u5f02\u5e38\u4f20\u64ad\u5230\u6790\u6784\u51fd\u6570\u5916\u90e8\u3002\u56e0\u4e3a\u5f53\u6790\u6784\u51fd\u6570\u5411\u5916\u629b\u51fa\u5f02\u5e38\u65f6&#xff0c;\u5c06\u76f4\u63a5\u8c03\u7528terminate()\u7cfb\u7edf\u51fd\u6570\u7ec8\u6b62\u7a0b\u5e8f\u6267\u884c\u3002<\/p>\n<h3>\u6027\u80fd\u4f18\u5316\u8003\u8651<\/h3>\n<p>\u200b\u200b\u5e73\u51e1\u6790\u6784\u51fd\u6570\u7684\u4f18\u5316\u200b\u200b&#xff1a;\u5bf9\u4e8e\u7b80\u5355\u7684\u7c7b&#xff08;\u4ec5\u5305\u542b\u57fa\u672c\u7c7b\u578b\u6210\u5458&#xff09;&#xff0c;\u4e0d\u5b9a\u4e49\u6790\u6784\u51fd\u6570\u6216\u4f7f\u7528&#061; default\u8bed\u6cd5\u53ef\u4ee5\u8ba9\u7f16\u8bd1\u5668\u8fdb\u884c\u66f4\u591a\u4f18\u5316\u3002<\/p>\n<p>\u200b\u200b\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6790\u6784\u8c03\u7528\u200b\u200b&#xff1a;\u6ce8\u610f\u9690\u5f0f\u62f7\u8d1d\u548c\u7c7b\u578b\u8f6c\u6362\u53ef\u80fd\u5bfc\u81f4\u7684\u4e0d\u5fc5\u8981\u6790\u6784\u8c03\u7528&#xff0c;\u4f8b\u5982\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u9002\u5f53\u7c7b\u578b\u7684\u5f15\u7528\u907f\u514d\u62f7\u8d1d&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u4e0d\u63a8\u8350 &#8211; \u53ef\u80fd\u5f15\u8d77\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\u548c\u6790\u6784<\/span><br \/>\n<span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span>std<span class=\"token double-colon punctuation\">::<\/span>string s<span class=\"token operator\">:<\/span> vec<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span> <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u63a8\u8350 &#8211; \u4f7f\u7528\u5f15\u7528\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\u548c\u6790\u6784<\/span><br \/>\n<span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> std<span class=\"token double-colon punctuation\">::<\/span>string<span class=\"token operator\">&amp;<\/span> s<span class=\"token operator\">:<\/span> vec<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span> <span class=\"token punctuation\">}<\/span><\/p>\n<p>\u200b\u200b\u667a\u80fd\u6307\u9488\u7684\u4f7f\u7528\u200b\u200b&#xff1a;\u5408\u7406\u4f7f\u7528std::unique_ptr\u548cstd::shared_ptr\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u91ca\u653e&#xff0c;\u51cf\u5c11\u624b\u52a8\u6790\u6784\u7684\u8d1f\u62c5\u3002<\/p>\n<h2>\u5178\u578b\u5e94\u7528\u573a\u666f\u603b\u7ed3<\/h2>\n<p>\u4e0b\u8868\u603b\u7ed3\u4e86\u6790\u6784\u51fd\u6570\u7684\u4e3b\u8981\u5e94\u7528\u573a\u666f&#xff1a;<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<tr>\u573a\u666f\u7c7b\u578b\u63cf\u8ff0\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>\u52a8\u6001\u5185\u5b58\u7ba1\u7406<\/td>\n<td>\u91ca\u653e\u5bf9\u8c61\u5185\u90e8\u52a8\u6001\u7533\u8bf7\u7684\u5185\u5b58<\/td>\n<td>delete[] ptr;<\/td>\n<\/tr>\n<tr>\n<td>\u8d44\u6e90\u91ca\u653e<\/td>\n<td>\u5173\u95ed\u6587\u4ef6\u3001\u91ca\u653e\u9501\u3001\u65ad\u5f00\u7f51\u7edc\u8fde\u63a5\u7b49<\/td>\n<td>fclose(file);<\/td>\n<\/tr>\n<tr>\n<td>\u65e5\u5fd7\u8bb0\u5f55<\/td>\n<td>\u5bf9\u8c61\u9500\u6bc1\u65f6\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f<\/td>\n<td>\u8f93\u51fa\u8c03\u8bd5\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td>\u5f15\u7528\u8ba1\u6570\u7ba1\u7406<\/td>\n<td>\u51cf\u5c11\u5171\u4eab\u8d44\u6e90\u7684\u5f15\u7528\u8ba1\u6570<\/td>\n<td>&#8211;ref_count;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u6790\u6784\u51fd\u6570\u7684\u57fa\u672c\u6982\u5ff5<br \/>\n\u4ec0\u4e48\u662f\u6790\u6784\u51fd\u6570<br \/>\n\u6790\u6784\u51fd\u6570&#xff08;destructor&#xff09;\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6210\u5458\u51fd\u6570&#xff0c;\u4e0e\u6784\u9020\u51fd\u6570\u529f\u80fd\u76f8\u53cd\u3002\u5f53\u5bf9\u8c61\u7ed3\u675f\u5176\u751f\u547d\u5468\u671f\u65f6&#xff08;\u5982\u5bf9\u8c61\u6240\u5728\u7684\u51fd\u6570\u5df2\u8c03\u7528\u5b8c\u6bd5&#xff09;&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u6267\u884c\u6790\u6784\u51fd\u6570\u3002\u6790\u6784\u51fd\u6570\u4e3b\u8981\u5b8c\u6210\\&#8221;\u6e05\u7406\u5584\u540e\\&#8221;\u5de5\u4f5c&#xff0c;\u4f8b\u5982\u91ca\u653e\u5bf9\u8c61\u5728\u751f\u547d\u5468\u671f\u4e2d\u7533\u8bf7\u7684\u8d44\u6e90\u3002<br \/>\n\u6790\u6784\u51fd\u6570\u7684\u58f0\u660e\u4e0e\u5b9a\u4e49<br \/>\n\u5728C\u4e2d&#xff0c;\u6790\u6784\u51fd\u6570\u7684\u58f0\u660e\u548c\u5b9a\u4e49\u9075\u5faa\u7279\u5b9a\u8bed\u6cd5\u89c4\u5219&#xff1a;<br \/>\nclass ClassN<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,190],"topic":[],"class_list":["post-74004","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010C++\u3011 \u6790\u6784\u51fd\u6570 - \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\/74004.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010C++\u3011 \u6790\u6784\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6790\u6784\u51fd\u6570\u7684\u57fa\u672c\u6982\u5ff5 \u4ec0\u4e48\u662f\u6790\u6784\u51fd\u6570 \u6790\u6784\u51fd\u6570&#xff08;destructor&#xff09;\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6210\u5458\u51fd\u6570&#xff0c;\u4e0e\u6784\u9020\u51fd\u6570\u529f\u80fd\u76f8\u53cd\u3002\u5f53\u5bf9\u8c61\u7ed3\u675f\u5176\u751f\u547d\u5468\u671f\u65f6&#xff08;\u5982\u5bf9\u8c61\u6240\u5728\u7684\u51fd\u6570\u5df2\u8c03\u7528\u5b8c\u6bd5&#xff09;&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u6267\u884c\u6790\u6784\u51fd\u6570\u3002\u6790\u6784\u51fd\u6570\u4e3b\u8981\u5b8c\u6210&quot;\u6e05\u7406\u5584\u540e&quot;\u5de5\u4f5c&#xff0c;\u4f8b\u5982\u91ca\u653e\u5bf9\u8c61\u5728\u751f\u547d\u5468\u671f\u4e2d\u7533\u8bf7\u7684\u8d44\u6e90\u3002 \u6790\u6784\u51fd\u6570\u7684\u58f0\u660e\u4e0e\u5b9a\u4e49 \u5728C\u4e2d&#xff0c;\u6790\u6784\u51fd\u6570\u7684\u58f0\u660e\u548c\u5b9a\u4e49\u9075\u5faa\u7279\u5b9a\u8bed\u6cd5\u89c4\u5219&#xff1a; class ClassN\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/74004.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-08T14:33:00+00:00\" \/>\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=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/74004.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/74004.html\",\"name\":\"\u3010C++\u3011 \u6790\u6784\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-08T14:33:00+00:00\",\"dateModified\":\"2026-02-08T14:33:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/74004.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/74004.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/74004.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010C++\u3011 \u6790\u6784\u51fd\u6570\"}]},{\"@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 \u6790\u6784\u51fd\u6570 - \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\/74004.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010C++\u3011 \u6790\u6784\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6790\u6784\u51fd\u6570\u7684\u57fa\u672c\u6982\u5ff5 \u4ec0\u4e48\u662f\u6790\u6784\u51fd\u6570 \u6790\u6784\u51fd\u6570&#xff08;destructor&#xff09;\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6210\u5458\u51fd\u6570&#xff0c;\u4e0e\u6784\u9020\u51fd\u6570\u529f\u80fd\u76f8\u53cd\u3002\u5f53\u5bf9\u8c61\u7ed3\u675f\u5176\u751f\u547d\u5468\u671f\u65f6&#xff08;\u5982\u5bf9\u8c61\u6240\u5728\u7684\u51fd\u6570\u5df2\u8c03\u7528\u5b8c\u6bd5&#xff09;&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u6267\u884c\u6790\u6784\u51fd\u6570\u3002\u6790\u6784\u51fd\u6570\u4e3b\u8981\u5b8c\u6210\"\u6e05\u7406\u5584\u540e\"\u5de5\u4f5c&#xff0c;\u4f8b\u5982\u91ca\u653e\u5bf9\u8c61\u5728\u751f\u547d\u5468\u671f\u4e2d\u7533\u8bf7\u7684\u8d44\u6e90\u3002 \u6790\u6784\u51fd\u6570\u7684\u58f0\u660e\u4e0e\u5b9a\u4e49 \u5728C\u4e2d&#xff0c;\u6790\u6784\u51fd\u6570\u7684\u58f0\u660e\u548c\u5b9a\u4e49\u9075\u5faa\u7279\u5b9a\u8bed\u6cd5\u89c4\u5219&#xff1a; class ClassN","og_url":"https:\/\/www.wsisp.com\/helps\/74004.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-08T14:33:00+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/74004.html","url":"https:\/\/www.wsisp.com\/helps\/74004.html","name":"\u3010C++\u3011 \u6790\u6784\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-08T14:33:00+00:00","dateModified":"2026-02-08T14:33:00+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/74004.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/74004.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/74004.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010C++\u3011 \u6790\u6784\u51fd\u6570"}]},{"@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\/74004","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=74004"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/74004\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=74004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=74004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=74004"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=74004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}