{"id":1478,"date":"2026-06-03T16:41:22","date_gmt":"2026-06-03T08:41:22","guid":{"rendered":"https:\/\/cqfdreamgarden.art\/?p=1478"},"modified":"2026-06-04T22:48:12","modified_gmt":"2026-06-04T14:48:12","slug":"%e8%87%aa%e5%ae%9a%e4%b9%89%e6%8b%b7%e8%b4%9d%e6%9e%84%e9%80%a0%e3%80%81%e6%8b%b7%e8%b4%9d%e8%b5%8b%e5%80%bc%e8%bf%90%e7%ae%97%e7%ac%a6%e5%92%8c%e6%9e%90%e6%9e%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/cqfdreamgarden.art\/index.php\/2026\/06\/03\/%e8%87%aa%e5%ae%9a%e4%b9%89%e6%8b%b7%e8%b4%9d%e6%9e%84%e9%80%a0%e3%80%81%e6%8b%b7%e8%b4%9d%e8%b5%8b%e5%80%bc%e8%bf%90%e7%ae%97%e7%ac%a6%e5%92%8c%e6%9e%90%e6%9e%84%e5%87%bd%e6%95%b0\/","title":{"rendered":"\u81ea\u5b9a\u4e49\u62f7\u8d1d\u6784\u9020\u3001\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u548c\u6790\u6784\u51fd\u6570"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u4e00\u4e2a\u5178\u578b\u7684\u62e5\u6709\u52a8\u6001\u8d44\u6e90\u7684\u7c7b\uff0c\u5b83\u5fc5\u987b\u81ea\u5b9a\u4e49\u62f7\u8d1d\u6784\u9020\u3001\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u548c\u6790\u6784\u51fd\u6570\u3002\u4ee3\u7801\u4e2d\u9644\u6709\u8be6\u7ec6\u6ce8\u91ca\uff0c\u540e\u9762\u4f1a\u8bb2\u89e3\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u4f7f\u7528\u9ed8\u8ba4\u7248\u672c\uff0c\u5e76\u603b\u7ed3\u8fd9\u7c7b\u51fd\u6570\u7684\u4e00\u822c\u683c\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;cstring>   \/\/ std::strlen, std::strcpy<br>#include &lt;iostream><br><br>\/\/ \u53ea\u5f15\u5165\u9700\u8981\u7684\u540d\u5b57\uff0c\u907f\u514d\u547d\u540d\u7a7a\u95f4\u6c61\u67d3<br>using std::strlen;<br>using std::strcpy;<br>using std::cout;<br>using std::endl;<br><br>class MyString {<br>private:<br>    char* m_data;   \/\/ \u6307\u5411\u5806\u4e0a\u5206\u914d\u7684\u5b57\u7b26\u4e32<br><br>public:<br>    \/\/ ---------- \u666e\u901a\u6784\u9020\u51fd\u6570 ----------<br>    \/\/ \u4ece C \u98ce\u683c\u5b57\u7b26\u4e32\u521b\u5efa MyString \u5bf9\u8c61<br>    MyString(const char* str = \"\") {<br>        if (str == nullptr) str = \"\";<br>        \/\/ \u5206\u914d\u8db3\u591f\u5bb9\u7eb3\u5b57\u7b26\u4e32 + \u7ed3\u5c3e '\\0' \u7684\u5185\u5b58<br>        m_data = new char[strlen(str) + 1];<br>        \/\/ \u5c06\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u5185\u5bb9\u590d\u5236\u5230\u81ea\u5df1\u7684\u5185\u5b58\u4e2d<br>        strcpy(m_data, str);<br>    }<br><br>    \/\/ ---------- \u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u6df1\u62f7\u8d1d\uff09 ----------<br>    \/\/ \u4ece\u53e6\u4e00\u4e2a MyString \u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u72ec\u7acb\u526f\u672c<br>    MyString(const MyString&amp; other) {<br>        \/\/ \u4e3a\u65b0\u5bf9\u8c61\u5206\u914d\u72ec\u7acb\u7684\u5185\u5b58<br>        m_data = new char[strlen(other.m_data) + 1];<br>        \/\/ \u5c06 other \u7684\u5b57\u7b26\u4e32\u5185\u5bb9\u590d\u5236\u8fc7\u6765<br>        strcpy(m_data, other.m_data);<br>        \/\/ \u6ce8\u610f\uff1a\u8fd9\u91cc\u6ca1\u6709\u76f4\u63a5\u8d4b\u503c\u6307\u9488\uff0c\u800c\u662f\u590d\u5236\u4e86\u6570\u636e\uff0c\u56e0\u6b64\u4e24\u4e2a\u5bf9\u8c61\u4e92\u76f8\u72ec\u7acb<br>    }<br><br>    \/\/ ---------- \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08\u6df1\u62f7\u8d1d\uff09 ----------<br>    \/\/ \u5c06\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684 MyString \u5bf9\u8c61\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684 MyString \u5bf9\u8c61<br>    MyString&amp; operator=(const MyString&amp; other) {<br>        \/\/ 1. \u68c0\u67e5\u81ea\u8d4b\u503c\uff1a\u5982\u679c\u81ea\u5df1\u8d4b\u503c\u7ed9\u81ea\u5df1\uff0c\u76f4\u63a5\u8fd4\u56de\uff0c\u907f\u514d\u91ca\u653e\u81ea\u8eab\u8d44\u6e90<br>        if (this != &amp;other) {<br>            \/\/ 2. \u91ca\u653e\u5f53\u524d\u5bf9\u8c61\u539f\u6709\u7684\u5185\u5b58\u8d44\u6e90<br>            delete[] m_data;<br><br>            \/\/ 3. \u50cf\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e00\u6837\u5206\u914d\u65b0\u5185\u5b58\u5e76\u590d\u5236\u6570\u636e<br>            m_data = new char[strlen(other.m_data) + 1];<br>            strcpy(m_data, other.m_data);<br>        }<br>        \/\/ 4. \u8fd4\u56de\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u4ee5\u652f\u6301\u94fe\u5f0f\u8d4b\u503c\uff08\u5982 a = b = c\uff09<br>        return *this;<br>    }<br><br>    \/\/ ---------- \u6790\u6784\u51fd\u6570 ----------<br>    \/\/ \u5bf9\u8c61\u751f\u547d\u5468\u671f\u7ed3\u675f\u65f6\u81ea\u52a8\u8c03\u7528\uff0c\u7528\u4e8e\u91ca\u653e\u5206\u914d\u7684\u8d44\u6e90<br>    ~MyString() {<br>        delete[] m_data;   \/\/ \u91ca\u653e\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5206\u914d\u7684\u5185\u5b58<br>    }<br><br>    \/\/ \u8f85\u52a9\u65b9\u6cd5\uff1a\u83b7\u53d6 C \u98ce\u683c\u5b57\u7b26\u4e32\uff08\u4fbf\u4e8e\u6d4b\u8bd5\u8f93\u51fa\uff09<br>    const char* c_str() const {<br>        return m_data;<br>    }<br>};<br><br>\/\/ \u7b80\u5355\u6d4b\u8bd5<br>int main() {<br>    MyString s1(\"Hello\");<br>    MyString s2 = s1;           \/\/ \u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570<br>    MyString s3(\"World\");<br>    s3 = s1;                    \/\/ \u8c03\u7528\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26<br><br>    cout &lt;&lt; s1.c_str() &lt;&lt; endl;<br>    cout &lt;&lt; s2.c_str() &lt;&lt; endl;<br>    cout &lt;&lt; s3.c_str() &lt;&lt; endl;<br>    return 0;<br>}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u662f\u7a0b\u5e8f\u7684\u8fd0\u884c\u8f93\u51fa\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">text<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Hello\nHello\nHello<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u51fa\u89e3\u91ca<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>s1<\/code><\/strong>\u00a0\u7684\u5185\u5bb9\u662f\u00a0<code>\"Hello\"<\/code>\uff0c\u76f4\u63a5\u8f93\u51fa\u00a0<code>Hello<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>s2<\/code><\/strong>\u00a0\u901a\u8fc7\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4ece\u00a0<code>s1<\/code>\u00a0\u521b\u5efa\uff0c\u62e5\u6709\u72ec\u7acb\u7684\u5b57\u7b26\u4e32\u6570\u636e\uff0c\u503c\u4e5f\u662f\u00a0<code>\"Hello\"<\/code>\uff0c\u56e0\u6b64\u8f93\u51fa\u00a0<code>Hello<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>s3<\/code><\/strong>\u00a0\u6700\u521d\u662f\u00a0<code>\"World\"<\/code>\uff0c\u4f46\u968f\u540e\u901a\u8fc7\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u00a0<code>s3 = s1;<\/code>\u00a0\u88ab\u8d4b\u4e88\u00a0<code>s1<\/code>\u00a0\u7684\u5185\u5bb9\uff0c\u65e7\u7684\u00a0<code>\"World\"<\/code>\u00a0\u5185\u5b58\u88ab\u6b63\u786e\u91ca\u653e\uff0c\u65b0\u7684\u72ec\u7acb\u5b57\u7b26\u4e32\u00a0<code>\"Hello\"<\/code>\u00a0\u88ab\u590d\u5236\u8fc7\u6765\uff0c\u6240\u4ee5\u6700\u7ec8\u4e5f\u8f93\u51fa\u00a0<code>Hello<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\u4e09\u884c\u8f93\u51fa\u90fd\u662f&nbsp;<code>Hello<\/code>\uff0c\u8bc1\u660e\u4e86\u6df1\u62f7\u8d1d\u7684\u6b63\u786e\u6027\uff1a\u4e09\u4e2a\u5bf9\u8c61\u5404\u81ea\u62e5\u6709\u72ec\u7acb\u7684\u5185\u5b58\uff0c\u4f46\u5185\u5bb9\u90fd\u662f\u4ece&nbsp;<code>s1<\/code>&nbsp;\u590d\u5236\u8fc7\u6765\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u8981\u81ea\u5b9a\u4e49\uff0c\u800c\u4e0d\u80fd\u4f7f\u7528\u9ed8\u8ba4\u7684\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5b8c\u5168\u4e0d\u5199\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3001\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u548c\u6790\u6784\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u751f\u6210<strong>\u9ed8\u8ba4\u7248\u672c<\/strong>\u3002\u9ed8\u8ba4\u7248\u672c\u7684\u884c\u4e3a\u662f<strong>\u9010\u6210\u5458\u62f7\u8d1d\uff08\u6d45\u62f7\u8d1d\uff09<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u5c06\u00a0<code>other.m_data<\/code>\uff08\u4e00\u4e2a\u6307\u9488\uff09\u76f4\u63a5\u8d4b\u503c\u7ed9\u65b0\u5bf9\u8c61\u7684\u00a0<code>m_data<\/code>\u3002<\/li>\n\n\n\n<li>\u9ed8\u8ba4\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u540c\u6837\u76f4\u63a5\u590d\u5236\u6307\u9488\u3002<\/li>\n\n\n\n<li>\u9ed8\u8ba4\u6790\u6784\u51fd\u6570\u4ec0\u4e48\u4e5f\u4e0d\u505a\uff08\u5bf9\u4e8e\u00a0<code>char*<\/code>\u00a0\u8fd9\u79cd\u57fa\u672c\u7c7b\u578b\u6210\u5458\uff0c\u4e0d\u4f1a\u8c03\u7528\u00a0<code>delete<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6211\u4eec\u7684&nbsp;<code>MyString<\/code>&nbsp;\u7c7b\u4e2d\uff0c\u8fd9\u4f1a\u5e26\u6765\u4e24\u4e2a\u4e25\u91cd\u95ee\u9898\uff1a<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>\u91cd\u590d\u91ca\u653e\uff08double free\uff09<\/strong><br>\u4e24\u4e2a\u00a0<code>MyString<\/code>\u00a0\u5bf9\u8c61\u5185\u90e8\u7684\u00a0<code>m_data<\/code>\u00a0\u4f1a\u6307\u5411<strong>\u540c\u4e00\u5757\u5806\u5185\u5b58<\/strong>\u3002\u5f53\u4e24\u4e2a\u5bf9\u8c61\u5148\u540e\u6790\u6784\u65f6\uff0c\u7b2c\u4e00\u4e2a\u5bf9\u8c61\u7684\u6790\u6784\u51fd\u6570\u91ca\u653e\u4e86\u8fd9\u5757\u5185\u5b58\uff0c\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\u6790\u6784\u65f6\u53c8\u4f1a\u518d\u6b21\u5c1d\u8bd5\u91ca\u653e\u540c\u4e00\u5757\u5185\u5b58\uff0c\u5bfc\u81f4<strong>\u672a\u5b9a\u4e49\u884c\u4e3a<\/strong>\uff08\u901a\u5e38\u7a0b\u5e8f\u5d29\u6e83\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u4e92\u76f8\u5e72\u6270<\/strong><br>\u7531\u4e8e\u4e24\u4e2a\u5bf9\u8c61\u5171\u4eab\u540c\u4e00\u4efd\u5b57\u7b26\u4e32\u6570\u636e\uff0c\u901a\u8fc7\u5176\u4e2d\u4e00\u4e2a\u5bf9\u8c61\u4fee\u6539\u5185\u5bb9\uff08\u5982\u679c\u6709\u4fee\u6539\u63a5\u53e3\u7684\u8bdd\uff09\u4f1a\u5f71\u54cd\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u8fdd\u80cc\u4e86\u201c\u503c\u8bed\u4e49\u201d\u7684\u72ec\u7acb\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\uff0c\u53ea\u8981\u7c7b\u4e2d\u62e5\u6709\u6307\u5411\u52a8\u6001\u5206\u914d\u8d44\u6e90\u7684\u6307\u9488\uff08\u6216\u6587\u4ef6\u53e5\u67c4\u3001\u7f51\u7edc\u8fde\u63a5\u7b49\uff09\uff0c\u5c31\u5fc5\u987b<strong>\u663e\u5f0f\u5b9e\u73b0\u201c\u4e09\u6cd5\u5219\u201d<\/strong>\uff1a<br>\u5982\u679c\u4e00\u4e2a\u7c7b\u9700\u8981\u81ea\u5b9a\u4e49\u6790\u6784\u51fd\u6570\u3001\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6216\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\uff0c\u90a3\u4e48\u5b83\u51e0\u4e4e\u4e00\u5b9a\u9700\u8981\u81ea\u5b9a\u4e49\u53e6\u5916\u4e24\u4e2a\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u822c\u683c\u5f0f\u603b\u7ed3<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ClassName(const ClassName&amp; other)\n    : <em>\/* \u6309\u9700\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf *\/<\/em> \n{\n    <em>\/\/ \u6267\u884c\u201c\u6df1\u62f7\u8d1d\u201d\uff1a<\/em>\n    <em>\/\/ \u4e3a\u6307\u9488\u6210\u5458\u5206\u914d\u65b0\u5185\u5b58\uff0c\u5e76\u5c06 other \u5bf9\u5e94\u5185\u5b58\u7684\u5185\u5bb9\u590d\u5236\u8fc7\u6765\u3002<\/em>\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53c2\u6570\u5fc5\u987b\u662f\u00a0<code>const<\/code>\u00a0\u5f15\u7528\uff0c\u907f\u514d\u65e0\u610f\u4fee\u6539\u6e90\u5bf9\u8c61\u4e14\u907f\u514d\u65e0\u9650\u9012\u5f52\u3002<\/li>\n\n\n\n<li>\u4efb\u52a1\u662f<strong>\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u72ec\u7acb\u7684\u65b0\u5bf9\u8c61<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ClassName&amp; operator=(const ClassName&amp; other) {\n    if (this != &amp;other) {          <em>\/\/ 1. \u68c0\u67e5\u81ea\u8d4b\u503c<\/em>\n        <em>\/\/ 2. \u91ca\u653e\u5f53\u524d\u5bf9\u8c61\u6301\u6709\u7684\u65e7\u8d44\u6e90<\/em>\n        <em>\/\/ 3. \u6df1\u62f7\u8d1d other \u7684\u8d44\u6e90\u5230\u5f53\u524d\u5bf9\u8c61<\/em>\n    }\n    return *this;                  <em>\/\/ 4. \u8fd4\u56de\u81ea\u8eab\u5f15\u7528<\/em>\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u81ea\u8d4b\u503c\u68c0\u67e5\u662f\u5fc5\u9700\u7684\uff0c\u5426\u5219\u00a0<code>delete<\/code>\u00a0\u4e4b\u540e\u00a0<code>other<\/code>\u00a0\u7684\u6570\u636e\u53ef\u80fd\u5df2\u7ecf\u65e0\u6548\u3002<\/li>\n\n\n\n<li>\u8fd4\u56de\u00a0<code>*this<\/code>\u00a0\u7684\u5f15\u7528\u662f\u4e3a\u4e86\u652f\u6301\u94fe\u5f0f\u8d4b\u503c\uff08<code>a = b = c<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u66f4\u73b0\u4ee3\u3001\u66f4\u5b89\u5168\u7684\u5199\u6cd5\u662f\u00a0<strong>copy-and-swap<\/strong>\u00a0\u60ef\u7528\u6cd5\uff0c\u5b83\u80fd\u540c\u65f6\u89e3\u51b3\u81ea\u8d4b\u503c\u548c\u5f02\u5e38\u5b89\u5168\u95ee\u9898\uff0c\u4f46\u7ecf\u5178\u5f62\u5f0f\u5bf9\u7406\u89e3\u539f\u7406\u66f4\u91cd\u8981\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u6790\u6784\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~ClassName() {\n    <em>\/\/ \u91ca\u653e\u5bf9\u8c61\u6301\u6709\u7684\u6240\u6709\u52a8\u6001\u8d44\u6e90\uff08delete \u6307\u9488\u3001\u5173\u95ed\u6587\u4ef6\u7b49\uff09<\/em>\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u663e\u5f0f\u8c03\u7528\uff0c\u5bf9\u8c61\u79bb\u5f00\u4f5c\u7528\u57df\u6216\u00a0<code>delete<\/code>\u00a0\u65f6\u81ea\u52a8\u6267\u884c\u3002<\/li>\n\n\n\n<li>\u804c\u8d23\u662f\u6e05\u7406\u6784\u9020\u51fd\u6570\u6216\u5176\u5b83\u6210\u5458\u51fd\u6570\u7533\u8bf7\u7684\u8d44\u6e90\uff0c\u786e\u4fdd\u65e0\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u638c\u63e1\u5e76\u6b63\u786e\u5b9e\u73b0\u8fd9\u4e09\u4e2a\u51fd\u6570\uff0c\u662f C++ \u4e2d\u5b89\u5168\u5730\u7ba1\u7406\u52a8\u6001\u8d44\u6e90\u7684\u57fa\u7840\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-ca636a7e259e6f931cfc01c880000873 wp-block-paragraph\">**********************************************************************************************************************************************************************************<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">MyString(const char* str = \"\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u62ec\u53f7\u91cc\u5199&nbsp;<code>= \"\"<\/code>&nbsp;\u662f C++ \u7684<strong>\u9ed8\u8ba4\u53c2\u6570<\/strong>\uff08default argument\uff09\u8bed\u6cd5\u3002\u5b83\u8868\u8fbe\u7684\u610f\u601d\u662f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8c03\u7528\u6784\u9020\u51fd\u6570\u65f6<strong>\u6ca1\u6709<\/strong>\u63d0\u4f9b\u4efb\u4f55\u5b9e\u53c2\uff0c\u90a3\u4e48\u5f62\u53c2&nbsp;<code>str<\/code>&nbsp;\u5c31<strong>\u9ed8\u8ba4\u53d6\u503c\u4e3a&nbsp;<code>\"\"<\/code>\uff08\u5373\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u7684\u6307\u9488\uff09<\/strong>\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u8fd9\u6837\u5199\uff1f<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>\u652f\u6301\u65e0\u53c2\u6784\u9020\uff08\u9ed8\u8ba4\u6784\u9020\uff09<\/strong><br>\u6709\u4e86\u9ed8\u8ba4\u53c2\u6570\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u50cf\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a\u201c\u7a7a\u7684\u00a0<code>MyString<\/code>\u201d\u5bf9\u8c61\uff1acppMyString s; <em>\/\/ \u7b49\u4ef7\u4e8e MyString s(&#8220;&#8221;);<\/em>\u5982\u679c\u6ca1\u6709\u00a0<code>= \"\"<\/code>\uff0c\u800c\u53ea\u5199\u00a0<code>MyString(const char* str)<\/code>\uff0c\u90a3\u4e48\u521b\u5efa\u5bf9\u8c61\u65f6\u5c31<strong>\u5fc5\u987b<\/strong>\u663e\u5f0f\u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1acppMyString s; <em>\/\/ \u7f16\u8bd1\u9519\u8bef\uff1a\u7f3a\u5c11\u53c2\u6570<\/em> MyString s(&#8220;Hello&#8221;); <em>\/\/ \u6b63\u786e<\/em>\u53ef\u89c1\uff0c<code>= \"\"<\/code>\u00a0\u8ba9\u6784\u9020\u51fd\u6570\u517c\u5177\u4e86<strong>\u9ed8\u8ba4\u6784\u9020\u51fd\u6570<\/strong>\u7684\u529f\u80fd\u3002<\/li>\n\n\n\n<li><strong>\u8ba9\u5185\u90e8\u903b\u8f91\u7edf\u4e00\u3001\u5b89\u5168<\/strong><br>\u6784\u9020\u51fd\u6570\u5185\u90e8\u7b2c\u4e00\u6761\u5c31\u662f\uff1acppif (str == nullptr) str = &#8220;&#8221;;\u8fd9\u662f\u9632\u5fa1\u6027\u5904\u7406\uff1a\u5373\u4f7f\u8c03\u7528\u8005\u4e0d\u5c0f\u5fc3\u4f20\u5165\u4e86\u00a0<code>nullptr<\/code>\uff0c\u4e5f\u4f1a\u88ab\u7ea0\u6b63\u4e3a\u5b89\u5168\u7684\u7a7a\u5b57\u7b26\u4e32\u6307\u9488\u3002<br>\u73b0\u5728\uff0c\u5f53<strong>\u5b8c\u5168\u4e0d\u4f20\u53c2\u6570<\/strong>\u65f6\uff0c\u5f62\u53c2\u00a0<code>str<\/code>\u00a0\u76f4\u63a5\u53d6\u9ed8\u8ba4\u503c\u00a0<code>\"\"<\/code>\uff08\u4e00\u4e2a\u5408\u6cd5\u7684\u975e\u7a7a\u6307\u9488\uff09\uff0c\u8fde\u00a0<code>nullptr<\/code>\u00a0\u90fd\u4e0d\u53ef\u80fd\u51fa\u73b0\u3002\u8fd9\u6837\uff0c\u540e\u7eed\u7684\u00a0<code>strlen(str)<\/code>\u00a0\u548c\u00a0<code>strcpy<\/code>\u00a0\u90fd\u80fd\u5b89\u5168\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>\u4e3a\u4ec0\u4e48\u9ed8\u8ba4\u503c\u9009\u00a0<code>\"\"<\/code>\u00a0\u800c\u4e0d\u662f\u00a0<code>nullptr<\/code>\uff1f<\/strong><br>\u5982\u679c\u5199\u6210\u00a0<code>= nullptr<\/code>\uff0c\u90a3\u4e48\u65e0\u53c2\u6784\u9020\u65f6\u5185\u90e8\u4ecd\u8981\u4f9d\u8d56\u00a0<code>if (str == nullptr)<\/code>\u00a0\u6765\u4fee\u6b63\uff0c\u8fd9\u867d\u7136\u4e5f\u80fd\u5de5\u4f5c\uff0c\u4f46\u4e0d\u5982\u76f4\u63a5\u7ed9\u00a0<code>\"\"<\/code>\u00a0\u6765\u5f97\u5e72\u51c0\uff1a\n<ul class=\"wp-block-list\">\n<li>\u8bed\u4e49\u4e0a\uff0c\u201c\u7a7a\u7684 MyString\u201d \u5c31\u5e94\u8be5\u5bf9\u5e94<strong>\u7a7a\u5b57\u7b26\u4e32<\/strong>\uff0c\u800c\u4e0d\u662f\u7a7a\u6307\u9488\u3002<\/li>\n\n\n\n<li>\u907f\u514d\u4e86\u989d\u5916\u7684 nullptr \u68c0\u67e5\u4f9d\u8d56\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>= \"\"<\/code>&nbsp;\u5c31\u662f\u8ba9\u8fd9\u4e2a\u5355\u53c2\u6570\u6784\u9020\u51fd\u6570\u540c\u65f6\u80fd\u5145\u5f53<strong>\u9ed8\u8ba4\u6784\u9020\u51fd\u6570<\/strong>\uff0c\u4f7f&nbsp;<code>MyString s;<\/code>&nbsp;\u5408\u6cd5\uff0c\u5e76\u81ea\u7136\u5730\u521b\u5efa\u4e00\u4e2a\u5185\u5bb9\u4e3a\u7a7a\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u540c\u65f6\u4fdd\u6301\u5185\u90e8\u5b9e\u73b0\u7684\u7b80\u6d01\u4e0e\u5b89\u5168\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5178\u578b\u7684\u62e5\u6709\u52a8\u6001\u8d44\u6e90\u7684\u7c7b\uff0c\u5b83\u5fc5\u987b\u81ea\u5b9a\u4e49\u62f7\u8d1d\u6784\u9020\u3001\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u548c\u6790\u6784\u51fd\u6570\u3002\u4ee3\u7801\u4e2d\u9644\u6709\u8be6\u7ec6\u6ce8\u91ca\uff0c\u540e\u9762\u4f1a\u8bb2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1478","post","type-post","status-publish","format-standard","hentry","category-cpp"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/posts\/1478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/comments?post=1478"}],"version-history":[{"count":1,"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/posts\/1478\/revisions"}],"predecessor-version":[{"id":1525,"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/posts\/1478\/revisions\/1525"}],"wp:attachment":[{"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/media?parent=1478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/categories?post=1478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/tags?post=1478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}