{"id":123273,"date":"2026-11-06T22:56:51","date_gmt":"2026-11-06T14:56:51","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/123273.html"},"modified":"2026-11-06T22:56:51","modified_gmt":"2026-11-06T14:56:51","slug":"c%e7%a7%bb%e5%8a%a8%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/123273.html","title":{"rendered":"C++\u79fb\u52a8\u6784\u9020\u51fd\u6570\u8be6\u89e3"},"content":{"rendered":"<p>        \/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/<br \/>\n        #content_views .toc,<br \/>\n        \/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/<br \/>\n        #content_views.markdown_views &gt; p:empty:has(+ .toc),<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty,<br \/>\n        \/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/<br \/>\n        #content_views.htmledit_views #main-toc,<br \/>\n        #content_views.htmledit_views #hr-toc,<br \/>\n        #content_views.htmledit_views p[id*=\\&#8221;-toc\\&#8221;] {<br \/>\n          display: none !important;<br \/>\n        }<br \/>\n        \/* \u76ee\u5f55\u53bb\u6389\u540e\uff0c\u7d27\u8ddf\u7684\u9996\u4e2a\u6807\u9898\u4e0d\u518d\u591a\u51fa\u4e00\u5757\u4e0a\u8fb9\u8ddd *\/<br \/>\n        #content_views.markdown_views &gt; .toc + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + h4,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h4 {<br \/>\n          margin-top: 0 !important;<br \/>\n        }<\/p>\n<p>\u8981\u5f04\u660e\u767d\u79fb\u52a8\u6784\u9020\u51fd\u6570\u662f\u4ec0\u4e48&#xff0c;\u9700\u8981\u9996\u5148\u4e86\u89e3\u4ec0\u4e48\u662f\u5de6\u503c\u53f3\u503c\u53ca\u53f3\u503c\u5f15\u7528&#xff0c;\u5982\u679c\u4e0d\u4e86\u89e3&#xff0c;\u53ef\u4ee5\u5148\u770b\u8fd9\u7bc7\u6587\u7ae0&#xff1a;c&#043;&#043;\u5de6\u503c\u53f3\u503c\u548c\u53f3\u503c\u5f15\u7528<\/p>\n<h4>\u4ec0\u4e48\u662f\u79fb\u52a8\u6784\u9020\u51fd\u6570<\/h4>\n<p>\u79fb\u52a8\u6784\u9020\u51fd\u6570\u662fC&#043;&#043;11\u5f15\u5165\u7684\u4e00\u79cd\u7279\u6b8a\u6784\u9020\u51fd\u6570&#xff0c;\u7528\u4e8e\u4ece\u4e34\u65f6\u5bf9\u8c61&#xff08;\u53f3\u503c&#xff09;&#034;\u7a83\u53d6&#034;\u8d44\u6e90\u6765\u521d\u59cb\u5316\u65b0\u5bf9\u8c61&#xff0c;\u800c\u4e0d\u662f\u8fdb\u884c\u6602\u8d35\u7684\u6df1\u62f7\u8d1d\u3002<\/p>\n<p>class MyString {<br \/>\n    char* data;<br \/>\n    size_t size;<br \/>\npublic:<br \/>\n    \/\/ \u79fb\u52a8\u6784\u9020\u51fd\u6570&#xff1a;\u53c2\u6570\u662f\u53f3\u503c\u5f15\u7528 &amp;&amp;<br \/>\n    MyString(MyString&amp;&amp; other) noexcept<br \/>\n        : data(other.data), size(other.size) {<br \/>\n        other.data &#061; nullptr;  \/\/ \u5173\u952e&#xff1a;\u628a\u6e90\u5bf9\u8c61\u7684\u8d44\u6e90\u7f6e\u7a7a<br \/>\n        other.size &#061; 0;<br \/>\n    }<br \/>\n}; <\/p>\n<h4>\u4e8c\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u5b83<\/h4>\n<p>\u6838\u5fc3\u95ee\u9898&#xff1a;\u6df1\u62f7\u8d1d\u6602\u8d35<\/p>\n<p>\u8003\u8651 std::vector \u5185\u90e8\u6301\u6709\u4e00\u5757\u5806\u5185\u5b58&#xff1a;<\/p>\n<p>std::vector&lt;int&gt; v1 &#061; {1,2,3,&#8230;,1000000};<br \/>\nstd::vector&lt;int&gt; v2 &#061; std::move(v1);  \/\/ \u671f\u671b&#xff1a;\u8f6c\u79fb\u6307\u9488&#xff0c;O(1) <\/p>\n<p>\u5982\u679c\u7528\u62f7\u8d1d\u6784\u9020&#xff1a;\u9700\u8981\u91cd\u65b0\u5206\u914d100\u4e07\u4e2aint\u5e76\u9010\u4e2a\u590d\u5236 \u2192 O(n)&#xff0c;\u6162\u3002<\/p>\n<p>\u5982\u679c\u7528\u79fb\u52a8\u6784\u9020&#xff1a;\u53ea\u9700\u628a\u5185\u90e8\u6307\u9488\u4ea4\u7ed9\u65b0\u5bf9\u8c61&#xff0c;\u628a\u6e90\u5bf9\u8c61\u6307\u9488\u7f6e\u7a7a \u2192 O(1)&#xff0c;\u5feb\u3002<\/p>\n<p>\u5178\u578b\u89e6\u53d1\u573a\u666f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u51fd\u6570\u8fd4\u56de\u5c40\u90e8\u5bf9\u8c61&#xff08;\u8fd4\u56de\u503c\u4f18\u5316\u914d\u5408&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u5bb9\u5668\u6269\u5bb9\u65f6\u642c\u8fc1\u5143\u7d20&#xff08;vector \u91cd\u65b0\u5206\u914d&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u663e\u5f0f std::move \u8f6c\u79fb\u6240\u6709\u6743<\/p>\n<\/li>\n<li>\n<p>\u4f20\u9012\u4e34\u65f6\u5bf9\u8c61<\/p>\n<\/li>\n<\/ul>\n<p>std::vector&lt;std::string&gt; getStrings() {<br \/>\n    std::vector&lt;std::string&gt; v;<br \/>\n    \/\/ &#8230; \u586b\u5145\u5927\u91cf\u5b57\u7b26\u4e32<br \/>\n    return v;  \/\/ \u79fb\u52a8\u800c\u975e\u62f7\u8d1d&#xff08;RVO\/NRVO&#xff09;<br \/>\n} <\/p>\n<p>\u719f\u6089c&#043;&#043;\u7684\u8bdd&#xff0c;\u65e0\u8bba\u662f\u51fd\u6570\u4f20\u53c2\u8fd8\u662f\u8d4b\u503c\u7b49\u7b49&#xff0c;c&#043;&#043;\u5f88\u5bb9\u6613\u5c06\u53d8\u91cf\u62f7\u8d1d\u4e00\u4e2a\u526f\u672c\u53bb\u64cd\u4f5c\u526f\u672c&#xff0c;\u4f46\u662f\u5982\u679c\u4f60\u5728\u7c7b\u4e2d\u58f0\u660e\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570&#xff0c;\u7f16\u8bd1\u5668\u5c31\u4f1a\u5728\u6ee1\u8db3\u6761\u4ef6\u7684\u65f6\u5019\u5982\u8fd9\u91cc\u7684\u5178\u578b\u89e6\u53d1\u573a\u666f\u4e0d\u62f7\u8d1d\u800c\u662f\u79fb\u52a8&#xff0c;\u8fd9\u6837\u4ece\u6027\u80fd\u4e0a\u6765\u8bb2\u662f\u6709\u4f18\u5316\u7684<\/p>\n<\/p>\n<h4>\u4e09\u3001\u5982\u4f55\u6b63\u786e\u4f7f\u7528<\/h4>\n<h5>1. \u6807\u51c6\u5199\u6cd5&#xff08;\u542b noexcept&#xff09;<\/h5>\n<p>MyString.h<\/p>\n<p>#include &lt;cstddef&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;utility&gt;<br \/>\n#include &lt;windows.h&gt; \/\/ \u5fc5\u987b\u5305\u542b\u8fd9\u4e2a\u5934\u6587\u4ef6,\u4f7fvscode\u652f\u6301\u4e2d\u6587\u8f93\u51fa<\/p>\n<p>class MyString {<br \/>\npublic:<br \/>\n    explicit MyString(const char* text &#061; &#034;&#034;) {<br \/>\n        size &#061; text &#061;&#061; nullptr ? 0 : std::strlen(text);<br \/>\n        data &#061; new char[size &#043; 1];<br \/>\n        if (size &#061;&#061; 0) {<br \/>\n            data[0] &#061; &#039;\\\\0&#039;;<br \/>\n        } else {<br \/>\n            std::memcpy(data, text, size &#043; 1);<br \/>\n        }<\/p>\n<p>        std::cout &lt;&lt; &#034;[\u6784\u9020] \u5185\u5bb9&#061;\\\\&#034;&#034; &lt;&lt; data<br \/>\n                  &lt;&lt; &#034;\\\\&#034;, \u8d44\u6e90\u5730\u5740&#061;&#034; &lt;&lt; static_cast&lt;const void*&gt;(data) &lt;&lt; &#039;\\\\n&#039;;<br \/>\n    }<\/p>\n<p>    MyString(const MyString&amp; other)<br \/>\n        : data(new char[other.size &#043; 1]), size(other.size) {<br \/>\n        if (other.data &#061;&#061; nullptr) {<br \/>\n            data[0] &#061; &#039;\\\\0&#039;;<br \/>\n        } else {<br \/>\n            std::memcpy(data, other.data, size &#043; 1);<br \/>\n        }<\/p>\n<p>        std::cout &lt;&lt; &#034;[\u62f7\u8d1d\u6784\u9020] \u6df1\u62f7\u8d1d\u5230\u8d44\u6e90\u5730\u5740&#061;&#034;<br \/>\n                  &lt;&lt; static_cast&lt;const void*&gt;(data) &lt;&lt; &#039;\\\\n&#039;;<br \/>\n    }<\/p>\n<p>    MyString(MyString&amp;&amp; other) noexcept<br \/>\n        : data(other.data), size(other.size) {<br \/>\n        other.data &#061; nullptr;<br \/>\n        other.size &#061; 0;<\/p>\n<p>        std::cout &lt;&lt; &#034;[\u79fb\u52a8\u6784\u9020] \u63a5\u7ba1\u8d44\u6e90\u5730\u5740&#061;&#034;<br \/>\n                  &lt;&lt; static_cast&lt;const void*&gt;(data)<br \/>\n                  &lt;&lt; &#034;&#xff0c;\u5e76\u5c06\u539f\u5bf9\u8c61\u7f6e\u4e3a\u7a7a\\\\n&#034;;<br \/>\n    }<\/p>\n<p>    MyString&amp; operator&#061;(const MyString&amp; other) {<br \/>\n        if (this &#061;&#061; &amp;other) {<br \/>\n            return *this;<br \/>\n        }<\/p>\n<p>        char* newData &#061; new char[other.size &#043; 1];<br \/>\n        if (other.data &#061;&#061; nullptr) {<br \/>\n            newData[0] &#061; &#039;\\\\0&#039;;<br \/>\n        } else {<br \/>\n            std::memcpy(newData, other.data, other.size &#043; 1);<br \/>\n        }<\/p>\n<p>        delete[] data;<br \/>\n        data &#061; newData;<br \/>\n        size &#061; other.size;<\/p>\n<p>        std::cout &lt;&lt; &#034;[\u62f7\u8d1d\u8d4b\u503c] \u91ca\u653e\u65e7\u8d44\u6e90\u5e76\u6df1\u62f7\u8d1d\u5230\u8d44\u6e90\u5730\u5740&#061;&#034;<br \/>\n                  &lt;&lt; static_cast&lt;const void*&gt;(data) &lt;&lt; &#039;\\\\n&#039;;<br \/>\n        return *this;<br \/>\n    }<\/p>\n<p>    MyString&amp; operator&#061;(MyString&amp;&amp; other) noexcept {<br \/>\n        if (this &#061;&#061; &amp;other) {<br \/>\n            return *this;<br \/>\n        }<\/p>\n<p>        char* oldData &#061; data;<br \/>\n        data &#061; other.data;<br \/>\n        size &#061; other.size;<br \/>\n        other.data &#061; nullptr;<br \/>\n        other.size &#061; 0;<br \/>\n        delete[] oldData;<\/p>\n<p>        std::cout &lt;&lt; &#034;[\u79fb\u52a8\u8d4b\u503c] \u91ca\u653e\u65e7\u8d44\u6e90\u5e76\u63a5\u7ba1\u8d44\u6e90\u5730\u5740&#061;&#034;<br \/>\n                  &lt;&lt; static_cast&lt;const void*&gt;(data)<br \/>\n                  &lt;&lt; &#034;&#xff0c;\u5e76\u5c06\u539f\u5bf9\u8c61\u7f6e\u4e3a\u7a7a\\\\n&#034;;<br \/>\n        return *this;<br \/>\n    }<\/p>\n<p>    ~MyString() {<br \/>\n        if (data !&#061; nullptr) {<br \/>\n            std::cout &lt;&lt; &#034;[\u6790\u6784] \u91ca\u653e\u8d44\u6e90\u5730\u5740&#061;&#034;<br \/>\n                      &lt;&lt; static_cast&lt;const void*&gt;(data)<br \/>\n                      &lt;&lt; &#034;&#xff0c;\u5185\u5bb9&#061;\\\\&#034;&#034; &lt;&lt; data &lt;&lt; &#034;\\\\&#034;\\\\n&#034;;<br \/>\n        } else {<br \/>\n            std::cout &lt;&lt; &#034;[\u6790\u6784] \u7a7a\u5bf9\u8c61\u65e0\u9700\u91ca\u653e\u8d44\u6e90\\\\n&#034;;<br \/>\n        }<br \/>\n        delete[] data;<br \/>\n    }<\/p>\n<p>    const char* c_str() const {<br \/>\n        return data &#061;&#061; nullptr ? &#034;&#034; : data;<br \/>\n    }<\/p>\n<p>    std::size_t length() const {<br \/>\n        return size;<br \/>\n    }<\/p>\n<p>    void print(const char* label) const {<br \/>\n        std::cout &lt;&lt; label &lt;&lt; &#034;: \u5185\u5bb9&#061;\\\\&#034;&#034; &lt;&lt; c_str()<br \/>\n                  &lt;&lt; &#034;\\\\&#034;, \u957f\u5ea6&#061;&#034; &lt;&lt; size<br \/>\n                  &lt;&lt; &#034;, \u8d44\u6e90\u5730\u5740&#061;&#034; &lt;&lt; static_cast&lt;const void*&gt;(data) &lt;&lt; &#039;\\\\n&#039;;<br \/>\n    }<\/p>\n<p>private:<br \/>\n    char* data;<br \/>\n    std::size_t size;<br \/>\n}; <\/p>\n<p>TestMove.cpp<\/p>\n<p>#include &#034;MyString.h&#034;<\/p>\n<p>int main() {<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\u4e3a UTF-8<br \/>\n    SetConsoleOutputCP(65001); <\/p>\n<p>    MyString source(&#034;Hello, move semantics!&#034;);<br \/>\n    source.print(&#034;source&#034;);<\/p>\n<p>    std::cout &lt;&lt; &#034;\\\\n&#061;&#061;&#061;&#061;&#061; 1. \u79fb\u52a8\u6784\u9020&#xff1a;\u4ea4\u51fa\u8d44\u6e90\u6240\u6709\u6743 &#061;&#061;&#061;&#061;&#061;&#034; &lt;&lt; &#039;\\\\n&#039;;<br \/>\n    MyString moved(std::move(source));<br \/>\n    source.print(&#034;source&#xff08;\u79fb\u52a8\u540e&#xff09;&#034;);<br \/>\n    moved.print(&#034;moved&#034;);<\/p>\n<p>    std::cout &lt;&lt; &#034;\\\\n&#061;&#061;&#061;&#061;&#061; 2. \u79fb\u52a8\u8d4b\u503c&#xff1a;\u63a5\u7ba1\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u8d44\u6e90 &#061;&#061;&#061;&#061;&#061;&#034; &lt;&lt; &#039;\\\\n&#039;;<br \/>\n    MyString target(&#034;old data&#034;);<br \/>\n    target.print(&#034;target&#xff08;\u79fb\u52a8\u524d&#xff09;&#034;);<br \/>\n    target &#061; std::move(moved);<br \/>\n    moved.print(&#034;moved&#xff08;\u79fb\u52a8\u540e&#xff09;&#034;);<br \/>\n    target.print(&#034;target&#xff08;\u79fb\u52a8\u540e&#xff09;&#034;);<\/p>\n<p>    std::cout &lt;&lt; &#034;\\\\n&#061;&#061;&#061;&#061;&#061; 3. \u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\u53ef\u4ee5\u91cd\u65b0\u8d4b\u503c &#061;&#061;&#061;&#061;&#061;&#034; &lt;&lt; &#039;\\\\n&#039;;<br \/>\n    moved &#061; MyString(&#034;reused&#034;);<br \/>\n    moved.print(&#034;moved&#xff08;\u91cd\u65b0\u8d4b\u503c\u540e&#xff09;&#034;);<\/p>\n<p>    std::cout &lt;&lt; &#034;\\\\n&#061;&#061;&#061;&#061;&#061; 4. main \u7ed3\u675f&#xff0c;\u5bf9\u8c61\u6309\u76f8\u53cd\u987a\u5e8f\u6790\u6784 &#061;&#061;&#061;&#061;&#061;&#034; &lt;&lt; &#039;\\\\n&#039;;<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u53ef\u4ee5\u5c06\u4ee3\u7801\u62f7\u8d1d\u5230IDE\u8fd0\u884c\u4e00\u4e0b\u770b\u770b\u8fd0\u884c\u7ed3\u679c&#xff0c;\u8fd0\u884c\u7ed3\u679c\u6587\u5b57\u8f83\u591a\u5c31\u4e0d\u8d34\u51fa\u6765\u4e86\u4ee5\u514d\u5f71\u54cd\u4e0b\u9762\u7684\u9605\u8bfb<\/p>\n<h5>2. \u5173\u952e\u8981\u70b9<\/h5>\n<table>\n<tr>\u8981\u70b9\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u53c2\u6570\u7c7b\u578b<\/td>\n<td>T&amp;&amp;&#xff08;\u53f3\u503c\u5f15\u7528&#xff09;&#xff0c;\u4e0d\u662f const T&amp;&amp;<\/td>\n<\/tr>\n<tr>\n<td>\u6807\u8bb0 noexcept<\/td>\n<td>std::vector \u6269\u5bb9\u65f6\u53ea\u6709\u5728\u79fb\u52a8\u6784\u9020\u4e3a noexcept \u65f6\u624d\u4f1a\u7528\u79fb\u52a8&#xff0c;\u5426\u5219\u9000\u5316\u4e3a\u62f7\u8d1d<\/td>\n<\/tr>\n<tr>\n<td>\u6e90\u5bf9\u8c61\u7f6e\u4e3a\u6709\u6548\u72b6\u6001<\/td>\n<td>\u5fc5\u987b\u8ba9 other \u5904\u4e8e\u53ef\u5b89\u5168\u6790\u6784\u7684\u72b6\u6001&#xff08;\u901a\u5e38\u7f6e\u7a7a\u6307\u9488\/\u6e05\u7a7a&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u81ea\u8d4b\u503c\u68c0\u67e5<\/td>\n<td>\u79fb\u52a8\u8d4b\u503c\u4e2d\u8981\u68c0\u67e5 this !&#061; &amp;other<\/td>\n<\/tr>\n<tr>\n<td>\u4e94\u6cd5\u5219<\/td>\n<td>\u5b9a\u4e49\u4e86\u6790\u6784\/\u62f7\u8d1d\/\u79fb\u52a8\u4e4b\u4e00&#xff0c;\u901a\u5e38\u9700\u8003\u8651\u5168\u90e8\u4e94\u4e2a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u63d0\u4e00\u4e0b\u5176\u4e2d\u7684\u4e24\u70b9<\/p>\n<p>\u8ba9\u6211\u4eec\u56de\u5230\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684\u5176\u4e2d\u4e00\u4e2a\u7247\u6bb5<\/p>\n<p>MyString(MyString&amp;&amp; other) noexcept<br \/>\n        : data(other.data), size(other.size) {<br \/>\n        other.data &#061; nullptr;<br \/>\n        other.size &#061; 0;<\/p>\n<p>        std::cout &lt;&lt; &#034;[\u79fb\u52a8\u6784\u9020] \u63a5\u7ba1\u8d44\u6e90\u5730\u5740&#061;&#034;<br \/>\n                  &lt;&lt; static_cast&lt;const void*&gt;(data)<br \/>\n                  &lt;&lt; &#034;&#xff0c;\u5e76\u5c06\u539f\u5bf9\u8c61\u7f6e\u4e3a\u7a7a\\\\n&#034;;<br \/>\n    } <\/p>\n<p>other.data &#061; nullptr; other.size &#061; 0;<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u628a\u539f\u5bf9\u8c61\u7f6e\u7a7a&#xff1f;\u56e0\u4e3a\u539f\u5bf9\u8c61\u662f\u4e2a\u53f3\u503c&#xff0c;\u9a6c\u4e0a\u5c31\u8981\u88ab\u9500\u6bc1\u4e86\u3002\u4f46\u5b83\u7684\u6790\u6784\u51fd\u6570\u8fd8\u662f\u4f1a\u8c03\u7528\u7684\u3002\u5982\u679c\u4e0d\u7f6e\u7a7a&#xff0c;\u6790\u6784\u51fd\u6570\u5c31\u4f1a\u628a data_ \u7ed9 delete \u6389\u2014\u2014\u90a3\u65b0\u5bf9\u8c61\u5c31\u6210\u91ce\u6307\u9488\u4e86\u3002<\/p>\n<p>\u6790\u6784\u51fd\u6570\u8fd9\u4e48\u5199<\/p>\n<p>~MyString() {<br \/>\n        if (data !&#061; nullptr) {<br \/>\n            std::cout &lt;&lt; &#034;[\u6790\u6784] \u91ca\u653e\u8d44\u6e90\u5730\u5740&#061;&#034;<br \/>\n                      &lt;&lt; static_cast&lt;const void*&gt;(data)<br \/>\n                      &lt;&lt; &#034;&#xff0c;\u5185\u5bb9&#061;\\\\&#034;&#034; &lt;&lt; data &lt;&lt; &#034;\\\\&#034;\\\\n&#034;;<br \/>\n        } else {<br \/>\n            std::cout &lt;&lt; &#034;[\u6790\u6784] \u7a7a\u5bf9\u8c61\u65e0\u9700\u91ca\u653e\u8d44\u6e90\\\\n&#034;;<br \/>\n        }<br \/>\n        delete[] data;<br \/>\n    } <\/p>\n<p>\u4ec0\u4e48\u662f\u4e94\u6cd5\u5219<\/p>\n<p>Rule of Five&#xff08;\u4e94\u6cd5\u5219&#xff0c;C&#043;&#043;11 \u53ca\u4ee5\u540e&#xff09; C&#043;&#043;11 \u5f15\u5165\u4e86\u79fb\u52a8\u8bed\u4e49&#xff0c;\u65b0\u589e\u4e86\u4e24\u4e2a\u7279\u6b8a\u6210\u5458\u51fd\u6570\u3002\u4e94\u6cd5\u5219\u5efa\u8bae&#xff0c;\u5982\u679c\u4f60\u9700\u8981\u5b9a\u4e49&#xff08;\u6216 &#061;delete&#xff09;\u4ee5\u4e0b\u4e94\u4e2a\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a&#xff0c;\u5c31\u5e94\u8be5\u8003\u8651\u5168\u90e8\u4e94\u4e2a&#xff1a;<\/p>\n<p>\u6790\u6784\u51fd\u6570<\/p>\n<p>\u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/p>\n<p>\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26<\/p>\n<p>\u79fb\u52a8\u6784\u9020\u51fd\u6570 (C&#043;&#043;11)<\/p>\n<p>\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 (C&#043;&#043;11) \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684MyString \u5199\u4e86\u90a3\u4e48\u591a\u7684\u539f\u56e0&#xff0c;MyString.h\u5c31\u662f\u9075\u5faa\u4e86\u4e94\u6cd5\u5219<\/p>\n<h5>3. \u7528 std::move \u5f3a\u5236\u8f6c\u79fb<\/h5>\n<p>Buffer b1(1024);<br \/>\nBuffer b2 &#061; std::move(b1);  \/\/ b1 \u4e4b\u540e\u4e0d\u5e94\u518d\u4f7f\u7528\u5176\u8d44\u6e90&#xff08;\u4f46\u53ef\u6790\u6784\/\u91cd\u65b0\u8d4b\u503c&#xff09;<\/p>\n<p>std::string s1 &#061; &#034;hello&#034;;<br \/>\nstd::string s2 &#061; std::move(s1);<br \/>\n\/\/ s1 \u73b0\u5728\u5904\u4e8e&#034;\u6709\u6548\u4f46\u672a\u6307\u5b9a&#034;\u72b6\u6001&#xff0c;\u901a\u5e38\u4e3a\u7a7a <\/p>\n<h5>4. \u5e38\u89c1\u9677\u9631<\/h5>\n<p>\/\/ \u274c \u9519\u8bef&#xff1a;\u79fb\u52a8\u6784\u9020\u6ca1\u7f6e\u7a7a\u6e90\u5bf9\u8c61 \u2192 \u53cc\u91cd\u91ca\u653e<br \/>\nBuffer(Buffer&amp;&amp; o) : ptr(o.ptr) {}  \/\/ o.ptr \u672a\u7f6e\u7a7a&#xff0c;\u6790\u6784\u65f6 delete \u4e24\u6b21<\/p>\n<p>\/\/ \u274c \u9519\u8bef&#xff1a;\u53c2\u6570\u5199\u6210 const T&amp;&amp;  \u2192 \u65e0\u6cd5\u4fee\u6539\u6e90\u5bf9\u8c61<br \/>\nBuffer(const Buffer&amp;&amp; o);  <\/p>\n<p>\/\/ \u274c \u9519\u8bef&#xff1a;\u79fb\u52a8\u6784\u9020\u672a\u6807 noexcept \u2192 vector \u6269\u5bb9\u8d70\u62f7\u8d1d<br \/>\nBuffer(Buffer&amp;&amp; o);  <\/p>\n<p>\/\/ \u274c \u5bf9 const \u5bf9\u8c61 std::move \u65e0\u6548 \u2192 \u9000\u5316\u4e3a\u62f7\u8d1d<br \/>\nconst std::string cs &#061; &#034;x&#034;;<br \/>\nstd::string s &#061; std::move(cs);  \/\/ \u5b9e\u9645\u8c03\u7528\u62f7\u8d1d\u6784\u9020 <\/p>\n<h5>5. \u4f55\u65f6\u8be5\u5199\/\u4e0d\u8be5\u5199<\/h5>\n<ul>\n<li>\n<p>\u9700\u8981\u624b\u5199&#xff1a;\u7c7b\u81ea\u5df1\u7ba1\u7406\u8d44\u6e90&#xff08;\u88f8\u6307\u9488\u3001\u6587\u4ef6\u53e5\u67c4\u3001socket \u7b49&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u9700\u8981\u624b\u5199&#xff1a;\u6210\u5458\u90fd\u662f std::string\/std::vector\/\u667a\u80fd\u6307\u9488\u7b49&#xff0c;\u7f16\u8bd1\u5668\u751f\u6210\u7684\u9ed8\u8ba4\u79fb\u52a8\u6784\u9020\u5df2\u8db3\u591f<\/p>\n<\/li>\n<li>\n<p>&#061; default \u663e\u5f0f\u751f\u6210&#xff1a;<\/p>\n<\/li>\n<\/ul>\n<p>class Widget {<br \/>\n    std::string name;<br \/>\n    std::vector&lt;int&gt; data;<br \/>\npublic:<br \/>\n    Widget(Widget&amp;&amp;) &#061; default;<br \/>\n    Widget&amp; operator&#061;(Widget&amp;&amp;) &#061; default;<br \/>\n    \/\/ \u62f7\u8d1d\u4e5f default \u5373\u53ef<br \/>\n}; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content<\/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-123273","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>C++\u79fb\u52a8\u6784\u9020\u51fd\u6570\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\/123273.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++\u79fb\u52a8\u6784\u9020\u51fd\u6570\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/123273.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-06T14:56:51+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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/123273.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/123273.html\",\"name\":\"C++\u79fb\u52a8\u6784\u9020\u51fd\u6570\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-11-06T14:56:51+00:00\",\"dateModified\":\"2026-11-06T14:56:51+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/123273.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/123273.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/123273.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++\u79fb\u52a8\u6784\u9020\u51fd\u6570\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":"C++\u79fb\u52a8\u6784\u9020\u51fd\u6570\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\/123273.html","og_locale":"zh_CN","og_type":"article","og_title":"C++\u79fb\u52a8\u6784\u9020\u51fd\u6570\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content","og_url":"https:\/\/www.wsisp.com\/helps\/123273.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-11-06T14:56:51+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/123273.html","url":"https:\/\/www.wsisp.com\/helps\/123273.html","name":"C++\u79fb\u52a8\u6784\u9020\u51fd\u6570\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-11-06T14:56:51+00:00","dateModified":"2026-11-06T14:56:51+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/123273.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/123273.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/123273.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++\u79fb\u52a8\u6784\u9020\u51fd\u6570\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\/123273","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=123273"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/123273\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=123273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=123273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=123273"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=123273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}