{"id":5222,"date":"2025-06-11T13:05:01","date_gmt":"2025-06-11T05:05:01","guid":{"rendered":"https:\/\/www.diggoodbox.com\/blog\/?p=5222"},"modified":"2025-06-11T13:05:01","modified_gmt":"2025-06-11T05:05:01","slug":"c%e7%a7%bb%e5%8a%a8%e6%9e%84%e9%80%a0%e5%92%8c%e7%a7%bb%e5%8a%a8%e8%b5%8b%e5%80%bc%e6%a6%82%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/www.diggoodbox.com\/blog\/?p=5222","title":{"rendered":"C++\u79fb\u52a8\u6784\u9020\u548c\u79fb\u52a8\u8d4b\u503c\u6982\u5ff5"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff08Move Constructor\uff09<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff0c\u7528\u6765\u201c\u79fb\u52a8\u201d\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff08\u53f3\u503c\uff09\u7684\u8d44\u6e90\uff08\u5982\u52a8\u6001\u5185\u5b58\u3001\u6587\u4ef6\u53e5\u67c4\u7b49\uff09\u5230\u65b0\u5bf9\u8c61\u4e2d\uff0c\u800c\u4e0d\u662f\u8fdb\u884c\u8d44\u6e90\u7684\u6df1\u62f7\u8d1d\u3002<br><\/li>\n\n\n\n<li>\u901a\u8fc7\u63a5\u6536\u53f3\u503c\u5f15\u7528\u53c2\u6570\uff08Type&amp;&amp;\uff09\u5b9e\u73b0\u3002<br><\/li>\n\n\n\n<li>\u79fb\u52a8\u540e\uff0c\u6e90\u5bf9\u8c61\u901a\u5e38\u88ab\u7f6e\u4e8e\u6709\u6548\u4f46\u672a\u5b9a\u4e49\u72b6\u6001\uff08\u6bd4\u5982\u6307\u9488\u7f6e\u4e3a nullptr\uff09\uff0c\u4ee5\u907f\u514d\u8d44\u6e90\u91cd\u590d\u91ca\u653e\u3002<br><\/li>\n\n\n\n<li>\u76ee\u7684\u662f\u63d0\u9ad8\u6027\u80fd\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u590d\u5236\u5f00\u9500\u3002<br><\/li>\n<\/ul>\n\n\n\n<p><strong>\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\uff08Move Assignment Operator\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u662f\u4e00\u79cd\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u7528\u6765\u5c06\u4e00\u4e2a\u53f3\u503c\u5bf9\u8c61\u7684\u8d44\u6e90\u201c\u79fb\u52a8\u201d\u5230\u5df2\u6709\u5bf9\u8c61\u4e2d\uff0c\u7c7b\u4f3c\u79fb\u52a8\u6784\u9020\uff0c\u4f46\u9488\u5bf9\u8d4b\u503c\u573a\u666f\u3002<br><\/li>\n\n\n\n<li>\u4e5f\u662f\u901a\u8fc7\u53f3\u503c\u5f15\u7528\u53c2\u6570\u5b9e\u73b0\u3002<br><\/li>\n\n\n\n<li>\u5b9e\u73b0\u65f6\u9700\u8981\u5148\u91ca\u653e\u5f53\u524d\u5bf9\u8c61\u5df2\u6709\u8d44\u6e90\uff0c\u7136\u540e\u63a5\u7ba1\u6e90\u5bf9\u8c61\u8d44\u6e90\u3002<br><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>English: Move Constructor and Move Assignment Concept<\/strong><\/h3>\n\n\n\n<p><strong>Move Constructor<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A special constructor that \u201cmoves\u201d resources (like dynamic memory or file handles) from a temporary (rvalue) object to a new object instead of performing a deep copy.<br><\/li>\n\n\n\n<li>Implemented by taking an rvalue reference parameter (Type&amp;&amp;).<br><\/li>\n\n\n\n<li>After moving, the source object is left in a valid but unspecified state (e.g., pointers set to nullptr) to avoid double freeing.<br><\/li>\n\n\n\n<li>Aimed at improving performance by avoiding unnecessary copies.<br><\/li>\n<\/ul>\n\n\n\n<p><strong>Move Assignment Operator<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An assignment operator that \u201cmoves\u201d resources from an rvalue object to an existing object, similar to move constructor but for assignment.<br><\/li>\n\n\n\n<li>Also implemented using rvalue references.<br><\/li>\n\n\n\n<li>Typically releases current resources of the target object before taking ownership of the source\u2019s resources.<br><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b80\u5355\u793a\u4f8b Example<\/strong><\/h3>\n\n\n\n<p>class MyString {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;char* data;<\/p>\n\n\n\n<p>public:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u79fb\u52a8\u6784\u9020\u51fd\u6570<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;MyString(MyString&amp;&amp; other) noexcept : data(other.data) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other.data = nullptr;&nbsp; \/\/ \u79fb\u52a8\u540e\u7f6e\u7a7a\u6e90\u6307\u9488<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;MyString&amp; operator=(MyString&amp;&amp; other) noexcept {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (this != &amp;other) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete[] data; &nbsp; &nbsp; &nbsp; \/\/ \u91ca\u653e\u539f\u8d44\u6e90<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data = other.data; &nbsp; \/\/ \u63a5\u7ba1\u8d44\u6e90<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other.data = nullptr;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return *this;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;~MyString() { delete[] data; }<\/p>\n\n\n\n<p>};<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5bf9C++\u79fb\u52a8\u6784\u9020\u548c\u79fb\u52a8\u8d4b\u503c\u7b49\u6982\u5ff5\u8fdb\u884c\u4ecb\u7ecd\u548c\u8bf4\u660e\uff0c\u5927\u5bb6\u53ef\u53c2\u7167\u5b66\u4e60\u4e0e\u5b9e\u8df5\u3002<\/p>\n","protected":false},"author":1,"featured_media":5194,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[17],"tags":[20,21,33,34,23,22],"class_list":["post-5222","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-c","tag-21","tag-33","tag-34","tag-23","tag-22"],"_links":{"self":[{"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5222"}],"version-history":[{"count":1,"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5222\/revisions"}],"predecessor-version":[{"id":5223,"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5222\/revisions\/5223"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/5194"}],"wp:attachment":[{"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.diggoodbox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}