{"id":381,"date":"2020-04-05T18:23:55","date_gmt":"2020-04-05T10:23:55","guid":{"rendered":"https:\/\/www.notown.club\/?p=381"},"modified":"2023-05-28T17:39:55","modified_gmt":"2023-05-28T09:39:55","slug":"c-%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.notown.top\/?p=381","title":{"rendered":"C++  \u7c7b\uff08\u7b14\u8bb0\u4e00\uff09"},"content":{"rendered":"\n<p>\u7c7b\u5b9a\u4e49\u6210\u5458\u51fd\u6570\u7684\u65b9\u5f0f\u4e8e\u666e\u901a\u51fd\u6570\u5dee\u4e0d\u591a\uff0c\u6210\u5458\u51fd\u6570\u7684<strong>\u58f0\u660e\u5fc5\u987b\u5728\u7c7b\u5185\u90e8<\/strong>\uff0c\u5b83\u7684\u5b9a\u4e49\u5219\u65e2\u53ef\u4ee5\u5728\u7c7b\u7684\u5185\u90e8\uff0c\u4e5f\u53ef\u4ee5\u5728\u5916\u90e8\u3002<\/p>\n\n\n\n<p>\u770b\u4e00\u4e2a\u6210\u5458\u51fd\u6570\u7684\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>struct Sale_\n{\n    string bookNo;\n    string isbn() const\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;return bookNo;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<br><code>Sale_ a_s;<br>a_s.bookNo=\u201d123\u201d;<br>a_s.isbn();\/\/\u8fd4\u56dea_s\u7684bookNo<\/code><\/p>\n\n\n\n<p>\u4ed6\u5176\u5b9e\u662f\u9690\u5f0f\u5730\u8fd4\u56dea_s.bookNo\u7684\u3002\u6210\u5458\u51fd\u6570isbn\u901a\u8fc7\u4e00\u4e2athis\u7684\u989d\u5916\u9690\u5f0f\u53c2\u6570\u6765\u8bbf\u95ee\u5b83\u7684\u90a3\u4e2a\u5bf9\u8c61\u3002<br>\u6362\u800c\u8a00\u4e4b\uff0c\u6210\u5458\u51fd\u6570\u7684\u5b9a\u4e49\u5176\u5b9e\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>string isbn() const\n{\n&nbsp;&nbsp;&nbsp;&nbsp;return this-&gt;bookNo;\n}<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u8fd8\u5e94\u8be5\u770b\u5230\uff0c\u4e0a\u9762\u7684isbn\u6210\u5458\u51fd\u6570\uff0c\u5728\u5f62\u53c2\u5217\u8868\u540e\u9762\u8ddf\u4e86\u4e2a<strong>const<\/strong>\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u89e3\u91ca\u4e00\u4e0b\u5b83\u7684\u7528\u5904\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cthis\u7684\u7c7b\u578b\u662fSale_ * const\uff0c\u5c31\u662f\u8bf4\u6211\u4eec\u4e0d\u80fd\u628a\u5b83\u7ed1\u5b9a\u5728\u4e00\u4e2a\u5e38\u91cf\u5bf9\u8c61\u4e0a\uff0c\u518d\u8bf4\u900f\u4e00\u70b9\uff0c\u5c31\u662f<strong>\u5f53\u5bf9\u8c61\u662f\u4e00\u4e2a\u5e38\u91cf\u65f6\uff0c\u6211\u4eec\u5e76\u4e0d\u80fd\u8c03\u7528\u8be5\u6210\u5458\u51fd\u6570<\/strong>\uff08\u5426\u5219\u5c31\u901a\u8fc7\u6307\u9488\u6539\u53d8\u4e86\u4e00\u4e2a\u5e38\u91cf\u5566\uff09\u3002\u5982\u679c\u6211\u4eec\u5728\u5f62\u53c2\u5217\u8868\u540e\u9762\u52a0\u4e0a\u4e00\u4e2aconst\uff0c\u5219\u8868\u660ethis\u6307\u9488\u662f\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff08\u5e38\u91cf\u5bf9\u8c61\u90fd\u53ef\u4ee5\u8c03\u7528\u6b64\u51fd\u6570\u4e86\uff09\u3002\u6240\u4ee5\u8bf4\uff0c\u5f53\u6210\u5458\u51fd\u6570\u4e0d\u4f1a\u6539\u53d8\u5bf9\u8c61\u7684\u503c\u7684\u65f6\u5019\uff0c\u5f62\u53c2\u540e\u52a0\u4e2aconst\u4f1a\u6bd4\u8f83\u4fdd\u9669\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e00\u3001\u6784\u9020\u51fd\u6570<\/strong><\/p>\n\n\n\n<p>\u6bcf\u4e2a\u7c7b\u90fd\u5206\u522b\u5b9a\u4e49\u4e86\u5b83\u7684\u5bf9\u8c61\u88ab\u521d\u59cb\u5316\u7684\u65b9\u5f0f\uff0c\u7c7b\u4f1a\u901a\u8fc7\u4e00\u4e2a\u6216\u51e0\u4e2a\u7279\u6b8a\u7684\u6210\u5458\u51fd\u6570\u6765\u63a7\u5236\u5176\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u5de5\u7a0b\uff0c\u8fd9\u4e9b\u51fd\u6570\u5c31\u79f0\u6784\u9020\u51fd\u6570\u3002\u6784\u9020\u51fd\u6570\u7684\u4efb\u52a1\u662f\u521d\u59cb\u5316\u7c7b\u5bf9\u8c61\u7684\u6570\u636e\u6210\u5458\uff0c\u65e0\u8bba\u4f55\u65f6\u53ea\u8981\u7c7b\u7684\u5bf9\u8c61\u88ab\u521b\u5efa\uff0c\u5c31\u4f1a\u6267\u884c\u6784\u9020\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u5f53\u7c7b\u6ca1\u6709\u58f0\u660e\u4efb\u4f55\u6784\u9020\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u751f\u6210\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002<br>\u4e0b\u9762\u662f\u4e00\u4e2a\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>struct Sales_dara{\n&nbsp;&nbsp;&nbsp;&nbsp;Sales_data() = default;\n&nbsp;&nbsp;&nbsp;&nbsp;Sales_data(const std::string &amp;s):bookNo(s){}\n&nbsp;&nbsp;&nbsp;&nbsp;Sales_data(const std::string &amp;s, unsigned n, double p):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bookNo(s), units_sold(n), revenue(p*n) {}\n    Sales_data(std::istream &amp;);\n&nbsp;&nbsp;&nbsp;&nbsp;std::string bookNo;\n&nbsp;&nbsp;&nbsp;&nbsp;unsigned units_sold =0;\n&nbsp;&nbsp;&nbsp;&nbsp;double revenue =0.0;\n}<\/code><\/pre>\n\n\n\n<p>\u5206\u6790\u4e0a\u9762\u7684\u4ee3\u7801\uff0c<br><code>Sales_data() = default;<\/code>\u662f\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u56e0\u4e3a\u6211\u4eec\u65e2\u9700\u8981\u5176\u4ed6\u5f62\u5f0f\u7684\u6784\u9020\u51fd\u6570\uff0c\u4e5f\u9700\u8981\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\uff08\u53ea\u6709\u4e3a\u5185\u7f6e\u7c7b\u578b\u7684\u6570\u636e\u6210\u5458\u63d0\u4f9b\u4e86\u521d\u59cb\u503c\u5e76\u4e14\u7f16\u8bd1\u5668\u652f\u6301\u7c7b\u5185\u521d\u59cb\u503c\uff0c\u6b64\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u624d\u6709\u7528\uff09<br><code>Sales_data(const std::string &amp;s):bookNo(s){}<\/code><br>\u8fd9\u884c\u4ee3\u7801\u51fa\u73b0\u4e86\u5192\u53f7\u4ee5\u53ca\u5192\u53f7\u548c\u82b1\u62ec\u53f7\u4e4b\u95f4\u7684\u65b0\u5185\u5bb9\uff0c\u8fd9\u90e8\u5206\u5185\u5bb9\u79f0<strong>\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u5217\u8868<\/strong>\uff0c\u5b83\u8d1f\u8d23\u4e3a\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u7684\u4e00\u4e2a\u6216\u51e0\u4e2a\u6570\u636e\u6210\u5458\u8d4b\u521d\u503c\u3002\u4e0d\u540c\u6210\u5458\u7684\u521d\u59cb\u5316\u901a\u8fc7\u9017\u53f7\u9694\u5f00\u3002<br><code>Sales_data(const std::string &amp;s, unsigned n, double p):<br>&nbsp;&nbsp;&nbsp;&nbsp;bookNo(s), units_sold(n),revenue(p*n) {}<\/code><\/p>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u4e0a\u9762\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u51fd\u6570\u4f53\u90fd\u662f\u7a7a\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\u7684\u552f\u4e00\u76ee\u7684\u5c31\u662f\u4e3a\u6570\u636e\u6210\u5458\u8d4b\u521d\u503c\uff0c\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u4efb\u52a1\u8981\u6267\u884c\uff0c\u51fd\u6570\u4f53\u5c31\u662f\u4e3a\u7a7a\u7684\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e8c\u3001\u8bbf\u95ee\u63a7\u5236\u4e0e\u5c01\u88c5<\/strong><\/p>\n\n\n\n<p>\u5b9a\u4e49\u5728public\u8bf4\u660e\u7b26\u4e4b\u540e\u7684\u6210\u5458\u5728\u6574\u4e2a\u7a0b\u5e8f\u90fd\u80fd\u88ab\u8bbf\u95ee\uff0cpublic\u6210\u5458\u5b9a\u4e49\u7c7b\u7684\u63a5\u53e3\u3002<br>\u5b9a\u4e49\u5728private\u8bf4\u660e\u7b26\u4e4b\u540e\u7684\u6210\u5458\u53ef\u4ee5\u88ab\u7c7b\u7684\u6210\u5458\u51fd\u6570\u8bbf\u95ee\uff0c\u4f46\u662f\u4e0d\u80fd\u88ab\u4f7f\u7528\u8be5\u7c7b\u7684\u4ee3\u7801\u8bbf\u95ee\uff0c\u4e5f\u5c31\u662fprivate\u90e8\u5206\u5c01\u88c5\u4e86\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282\u3002<br><strong>class\u4e0estruct<\/strong><br>\u8fd9\u4e24\u4e2a\u5173\u952e\u5b57\u90fd\u53ef\u4ee5\u5b9a\u4e49\u7c7b\uff0c\u552f\u4e00\u7684\u533a\u522b\u5c31\u5728\u4e8e\uff0c\u4ed6\u4eec\u7684\u9ed8\u8ba4\u8bbf\u95ee\u6743\u9650\u4e0d\u4e00\u6837\u3002<br><strong>\u82e5\u4f7f\u7528struct\u5173\u952e\u5b57\uff0c\u5219\u5b9a\u4e49\u5728\u7b2c\u4e00\u4e2a\u8bbf\u95ee\u8bf4\u660e\u7b26\u4e4b\u524d\u7684\u6210\u5458\u90fd\u662fpublic<\/strong>\u7684\u3002<br><strong>\u82e5\u4f7f\u7528class\u5173\u952e\u5b57\uff0c\u5219\u5b9a\u4e49\u5728\u7b2c\u4e00\u4e2a\u8bbf\u95ee\u8bf4\u660e\u7b26\u4e4b\u524d\u7684\u6210\u5458\u90fd\u662fprivate<\/strong>\u7684\u3002<\/p>\n\n\n\n<p>\u5c01\u88c5\u7684\u4e24\u4e2a\u4f18\u70b9\uff1a<br>1\u3001\u786e\u4fdd\u7528\u6237\u4ee3\u7801\u4e0d\u4f1a\u65e0\u610f\u95f4\u7834\u574f\u5c01\u88c5\u5bf9\u8c61\u7684\u72b6\u6001\u3002<br>2\u3001\u88ab\u5c01\u88c5\u7684\u7c7b\u7684\u5177\u4f53\u5b9e\u73b0\u53ef\u4ee5\u968f\u65f6\u6539\u53d8\uff0c\u800c\u65e0\u9700\u8c03\u6574\u7528\u6237\u7ea7\u522b\u7684\u4ee3\u7801<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e09\u3001\u53cb\u5143<\/strong><\/p>\n\n\n\n<p>\u5bf9\u4e8e\u975e\u6210\u5458\u51fd\u6570\u6216\u8005\u662f\u5176\u5b83\u7c7b\uff0c\u4f46\u662f\u53c8\u60f3\u8bbf\u95ee\u5230\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u7684\u6210\u5458\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u4ee4\u5176\u5b83\u7c7b\u6216\u8005\u8fd9\u4e2a\u975e\u6210\u5458\u51fd\u6570\u6210\u4e3a\u8be5\u7c7b\u7684\u53cb\u5143\u3002\u5982\u679c\u662f\u5bf9\u4e8e\u51fd\u6570\uff0c\u53ea\u9700\u8981<strong>\u5728\u8be5\u7c7b\u91cc\u589e\u52a0\u4e00\u6761\u4ee5friend\u5173\u952e\u5b57\u5f00\u59cb\u7684\u51fd\u6570\u58f0\u660e<\/strong>\u8bed\u53e5\u5373\u53ef\uff08\u5e76\u4e0d\u4ee3\u8868\u8fd9\u4e2a\u51fd\u6570\u5728\u5916\u9762\u5c31\u4e0d\u7528\u518d\u58f0\u660e\u4e86\uff09\uff1b\u5982\u679c\u662f\u5bf9\u4e8e\u7c7b\uff0c\u4e5f\u662f\u5728\u9700\u8981\u88ab\u8bbf\u95ee\u5230\u79c1\u6709\u6210\u5458\u7684\u7c7b\u4e2d\uff0c\u589e\u52a0\u4e00\u6761\u4ee5friend\u5173\u952e\u5b57\u5f00\u59cb\u7684\u7c7b\u58f0\u660e\u3002<br><strong>\u5982\u679c\u5b58\u7740\u4e24\u4e2a\u7c7b\uff1aA\u548cB\uff0c\u6b64\u65f6A\u7684clear\u51fd\u6570\u60f3\u8981\u8bbf\u95eeB\u7684\u79c1\u6709\u6210\u5458\uff0c\u7c7b\u4e4b\u95f4\u5b9a\u4e49\u7684\u987a\u5e8f\u662f\u5982\u4f55\u7684\u5462\uff1f<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>A\u5148\u5b9a\u4e49\uff0c\u4f46\u662fclear\u51fd\u6570<strong>\u53ea\u6709\u58f0\u660e<\/strong>\uff08\u56e0\u4e3a\u6b64\u65f6\u8fd8\u65e0\u6cd5\u8bbf\u95eeB\u7684\u79c1\u6709\u6210\u5458\uff09<\/li><li>B\u518d\u5b9a\u4e49\uff0c\u5b9a\u4e49\u65f6\u5bf9A\u7684clear\u51fd\u6570\u8fdb\u884c\u53cb\u5143\u58f0\u660e\uff08\u4ee5\u4fbf\u540e\u7eed\u8be5\u51fd\u6570\u53ef\u4ee5\u8bbf\u95eeB\u7684\u79c1\u6709\u6210\u5458\uff09<\/li><li>\u5728\u5c06clear\u51fd\u6570\u8fdb\u884c\u7c7b\u5916\u5b9a\u4e49<\/li><\/ol>\n\n\n<h4>\u53cb\u5143\u58f0\u660e\u4e0d\u662f\u58f0\u660e\uff01<\/h4>\n<p>\u5176\u4ed6\u7c7b\u548c\u975e\u6210\u5458\u51fd\u6570\u60f3\u8981\u6210\u4e3a\u53cb\u5143\u65f6\uff0c\u4ed6\u4eec\u771f\u6b63\u7684\u58f0\u660e\u662f\u4e0d\u9700\u8981\u5728\u53cb\u5143\u58f0\u660e\u4e4b\u524d\u7684\uff0c\u4f46\u662f\uff0c\u4efb\u4f55\u5bf9\u4ed6\u4eec\u7684\u4f7f\u7528\u90fd\u5fc5\u987b\u8981<strong>\u5148\u8fdb\u884c\u771f\u6b63\u7684\u58f0\u660e<\/strong>\u3002<\/p>\n\n\n<pre class=\"wp-block-code\"><code>struct X{\n    friend void outerFunc() { cout &lt;&lt; \"hello world\" &lt;&lt; endl; }\/\/ \u8be5\u51fd\u6570\u5728\u8fdb\u884c\u53cb\u5143\u58f0\u660e\u7684\u540c\u65f6\uff0c\u8fdb\u884c\u4e86\u5b9a\u4e49\uff0c\u4f46\u5374\u6ca1\u6709\u771f\u6b63\u58f0\u660e\n    X() { outerFunc(); }\/\/ \u8fd9\u662f\u975e\u6cd5\u7684\uff0c\u56e0\u4e3aouterFunc\u6ca1\u88ab\u58f0\u660e\n    void g();\n    void h();\n};\n\nvoid X::g() { outerFunc(); }\/\/ \u975e\u6cd5\uff0c\u56e0\u4e3aouterFunc\u6ca1\u88ab\u58f0\u660e\nvoid outerFunc();\/\/ \u9996\u6b21\u51fa\u73b0\u4e86\u5bf9outerFunc\u7684\u58f0\u660e\nvoid X::h() { outerFunc(); }\/\/ \u6b63\u786e<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u56db\u3001\u53ef\u53d8\u6570\u636e\u6210\u5458<\/strong><\/p>\n\n\n\n<p>\u6709\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u80fd\u4fee\u6539\u7c7b\u7684\u67d0\u4e2a\u6570\u636e\u6210\u5458\uff0c\u5373\u4f7f\u662f\u5728\u4e00\u4e2aconst\u6210\u5458\u51fd\u6570\u5185\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u53ef\u4ee5\u5728\u53d8\u91cf\u7684\u58f0\u660e\u4e2d\u52a0\u5165mutable\u5173\u952e\u5b57\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>class Screen{<br>public:<br>&nbsp;&nbsp;&nbsp;&nbsp;void some_member() const;<br>private:<br>&nbsp;&nbsp;&nbsp;&nbsp;mutable size_t access_ctr;<br>};<br>void Screen::some_member() const<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;++access_ctr;\/\/ \u4fdd\u5b58\u4e00\u4e2a\u8ba1\u6570\u503c\uff0c\u7528\u4e8e\u8bb0\u5f55\u6210\u5458\u51fd\u6570\u88ab\u8c03\u7528\u7684\u6b21\u6570\u3002<br>&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u2026\u5728\u8fdb\u884c\u4e00\u4e9b\u5176\u5b83\u7684\u64cd\u4f5c<br>}<\/code><\/pre>\n\n\n\n<p>\u5982\u4e0a\u6240\u793a\uff0c\u5c3d\u7ba1some_member\u662f\u4e00\u4e2aconst\u6210\u5458\u51fd\u6570\uff0c\u4ed6\u4ecd\u7136\u80fd\u591f\u6539\u53d8access_ctr\u7684\u503c\u3002\u56e0\u4e3a\u8be5\u6210\u5458\u662f\u4e2a\u53ef\u53d8\u6210\u5458\uff0c\u5728\u4efb\u4f55\u6210\u5458\u51fd\u6570\u5185\uff08\u5305\u62ecconst\u51fd\u6570\u5728\u5185\uff09\u90fd\u53ef\u4ee5\u6539\u53d8\u8be5\u6210\u5458\u7684\u503c\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e94\u3001\u59d4\u6258\u6784\u9020\u51fd\u6570<\/strong><\/p>\n\n\n\n<p>\u59d4\u6258\u6784\u9020\u51fd\u6570\u5c31\u662f\u4f7f\u7528\u5b83\u6240\u5c5e\u7c7b\u7684\u5176\u5b83\u6784\u9020\u51fd\u6570\u6267\u884c\u5b83\u81ea\u5df1\u7684\u521d\u59cb\u5316\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>class Sales_data<br>{<br>public:<br>&nbsp;&nbsp;&nbsp;&nbsp;Sales_data(std::string s, unsigned cnt, double price):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bookNo(s),units_sold(cnt),revenue(cnt *price){}\/\/\u6b63\u5e38\u7684\u6784\u9020\u51fd\u6570<br>&nbsp;&nbsp;&nbsp;&nbsp;Sales_data():Sales_data(\u201c\u201d,0,0){}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u59d4\u6258\u6784\u9020\u51fd\u6570<br>&nbsp;&nbsp;&nbsp;&nbsp;Sales_data(std::string s): Sales_data(s,0,0){}&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u59d4\u6258\u6784\u9020\u51fd\u6570<br>&nbsp;&nbsp;&nbsp;&nbsp;std::string bookNo;<br>&nbsp;&nbsp;&nbsp;&nbsp;unsigned units_sold =0;<br>&nbsp;&nbsp;&nbsp;&nbsp;double revenue =0.0;<br>}<\/code><\/pre>\n\n\n<h3>\u516d\u3001\u9690\u5f0f\u7684\u7c7b\u7c7b\u578b\u8f6c\u6362<\/h3>\n<p>\u5982\u679c\u6784\u9020\u51fd\u6570<strong>\u53ea\u63a5\u53d7\u4e00\u4e2a\u5b9e\u53c2<\/strong>\uff0c\u5219\u5b83\u5b9e\u9645\u4e0a\u5b9a\u4e49\u4e86\u8f6c\u6362\u4e3a\u6b64\u7c7b\u7c7b\u578b\u7684\u9690\u5f0f\u8f6c\u6362\u673a\u5236\uff08\u4f46\u7f16\u8bd1\u5668<strong>\u53ea\u4f1a<\/strong>\u81ea\u52a8\u5730\u6267\u884c<strong>\u4e00\u6b65<\/strong>\u7c7b\u578b\u8f6c\u6362\uff09<\/p>\n\n\n<pre class=\"wp-block-code\"><code>struct myStructTest{\n    int a = 0;\n    string b = \"g\";\n    myStructTest &amp; combine(const myStructTest &amp;other){\n        this-&gt;a += other.a;\n        this-&gt;b += other.b;\n        return *this;\n    }\n    myStructTest(int x) :a(x){ }\/\/ \u5b58\u5728\u6574\u578b\u5230myStructTest\u5bf9\u8c61\u7684\u8f6c\u6362\n    myStructTest(string x) :b(x){ }\/\/ \u5b58\u5728string\u578b\u5230myStructTest\u5bf9\u8c61\u7684\u8f6c\u6362\n    myStructTest(int x, string y) :a(x), b(y){ }\/\/ \u4e00\u4e2a\u4ee5\u4e0a\u7684\u53c2\u6570\u5c31\u4e0d\u4f1a\u6709\u8f6c\u6362\n    myStructTest() = default;\n};\n\nmyStructTest ta;\/\/\u9ed8\u8ba4\u6784\u9020\u4e86ta\nta.combine(5);\/\/\u76f8\u5f53\u4e8e\u5148\u8f6c\u6362\u6210myStructTest(5)\uff0c\u7136\u540e\u518d\u628a\u5b83\u5f53\u521d\u53c2\u6570\nta.combine(\"5\");\/\/\u975e\u6cd5\uff01\uff01\uff01\uff0c\u56e0\u4e3a\"5\"\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff0c\u6216\u8005\u8bf4const char&#91;]\uff0c\u5b83\u65e0\u6cd5\u901a\u8fc7\u4e00\u5c42\u8f6c\u6362\uff0c\u53d8\u6210myStructTest\u7c7b\u3002\u9664\u975e\u8ba9string s = \"5\";\u518d\u8ba9s\u4f5c\u4e3a\u5b9e\u53c2<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5728\u53ea\u542b\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u524d\u9762\u7528<strong>explicit<\/strong>\u5173\u952e\u5b57\u8fdb\u884c\u58f0\u660e\uff0c\u5c31\u80fd\u963b\u6b62\u9690\u5f0f\u8f6c\u6362<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7c7b\u5b9a\u4e49\u6210\u5458\u51fd\u6570\u7684\u65b9\u5f0f\u4e8e\u666e\u901a\u51fd\u6570\u5dee\u4e0d\u591a\uff0c\u6210\u5458\u51fd\u6570\u7684\u58f0\u660e\u5fc5\u987b\u5728\u7c7b\u5185\u90e8\uff0c\u5b83\u7684\u5b9a\u4e49\u5219\u65e2\u53ef\u4ee5\u5728\u7c7b\u7684\u5185\u90e8\uff0c\u4e5f\u53ef\u4ee5\u5728\u5916\u90e8\u3002 &hellip;<a href=\"https:\/\/www.notown.top\/?p=381\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C++  \u7c7b\uff08\u7b14\u8bb0\u4e00\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[11],"tags":[],"class_list":["post-381","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=381"}],"version-history":[{"count":12,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":634,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/381\/revisions\/634"}],"wp:attachment":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}