{"id":2967,"date":"2020-12-21T12:49:53","date_gmt":"2020-12-21T04:49:53","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=2967"},"modified":"2020-12-21T12:49:53","modified_gmt":"2020-12-21T04:49:53","slug":"%e6%96%87%e6%a1%a3%e7%a7%bb%e5%8a%a8%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e5%92%8c%e7%a7%bb%e5%8a%a8%e8%b5%8b%e5%80%bc%e8%bf%90%e7%ae%97%e7%ac%a6-c","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=2967","title":{"rendered":"[\u6587\u6863]\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 (C++)"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\/\/ MemoryBlock.h\n#pragma once\n#include &lt;iostream>\n#include &lt;algorithm>\n\nclass MemoryBlock\n{\npublic:\n\n   \/\/ Simple constructor that initializes the resource.\n   explicit MemoryBlock(size_t length)\n      : _length(length)\n      , _data(new int[length])\n   {\n      std::cout &lt;&lt; \"In MemoryBlock(size_t). length = \"\n                &lt;&lt; _length &lt;&lt; \".\" &lt;&lt; std::endl;\n   }\n\n   \/\/ Destructor.\n   ~MemoryBlock()\n   {\n      std::cout &lt;&lt; \"In ~MemoryBlock(). length = \"\n                &lt;&lt; _length &lt;&lt; \".\";\n\n      if (_data != nullptr)\n      {\n         std::cout &lt;&lt; \" Deleting resource.\";\n         \/\/ Delete the resource.\n         delete[] _data;\n      }\n\n      std::cout &lt;&lt; std::endl;\n   }\n\n   \/\/ Copy constructor.\n   MemoryBlock(const MemoryBlock&amp; other)\n      : _length(other._length)\n      , _data(new int[other._length])\n   {\n      std::cout &lt;&lt; \"In MemoryBlock(const MemoryBlock&amp;). length = \"\n                &lt;&lt; other._length &lt;&lt; \". Copying resource.\" &lt;&lt; std::endl;\n\n      std::copy(other._data, other._data + _length, _data);\n   }\n\n   \/\/ Copy assignment operator.\n   MemoryBlock&amp; operator=(const MemoryBlock&amp; other)\n   {\n      std::cout &lt;&lt; \"In operator=(const MemoryBlock&amp;). length = \"\n                &lt;&lt; other._length &lt;&lt; \". Copying resource.\" &lt;&lt; std::endl;\n\n      if (this != &amp;other)\n      {\n         \/\/ Free the existing resource.\n         delete[] _data;\n\n         _length = other._length;\n         _data = new int[_length];\n         std::copy(other._data, other._data + _length, _data);\n      }\n      return *this;\n   }\n\n   \/\/ Retrieves the length of the data resource.\n   size_t Length() const\n   {\n      return _length;\n   }\n\nprivate:\n   size_t _length; \/\/ The length of the resource.\n   int* _data; \/\/ The resource.\n};<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"to-create-a-move-constructor-for-a-c-class\">\u4e3a C++ \u521b\u5efa\u79fb\u52a8\u6784\u9020\u51fd\u6570<\/h3>\n\n\n\n<p> \u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684\u6784\u9020\u51fd\u6570\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u91c7\u7528\u4e00\u4e2a\u5bf9\u7c7b\u7c7b\u578b\u7684\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u53c2\u6570\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u6240\u793a\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MemoryBlock(MemoryBlock&amp;&amp; other)\n   : _data(nullptr)\n   , _length(0)\n{\n}<\/code><\/pre>\n\n\n\n<p> \u5728\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u5c06\u6e90\u5bf9\u8c61\u4e2d\u7684\u7c7b\u6570\u636e\u6210\u5458\u6dfb\u52a0\u5230\u8981\u6784\u9020\u7684\u5bf9\u8c61\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_data = other._data;\n_length = other._length;<\/code><\/pre>\n\n\n\n<p> \u5c06\u6e90\u5bf9\u8c61\u7684\u6570\u636e\u6210\u5458\u5206\u914d\u7ed9\u9ed8\u8ba4\u503c\u3002\u00a0\u8fd9\u53ef\u4ee5\u9632\u6b62\u6790\u6784\u51fd\u6570\u591a\u6b21\u91ca\u653e\u8d44\u6e90\uff08\u5982\u5185\u5b58\uff09: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>other._data = nullptr;\nother._length = 0;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"to-create-a-move-assignment-operator-for-a-c-class\">\u4e3a C++ \u7c7b\u521b\u5efa\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26<\/h3>\n\n\n\n<p> \u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u8be5\u8fd0\u7b97\u7b26\u91c7\u7528\u4e00\u4e2a\u5bf9\u7c7b\u7c7b\u578b\u7684\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5bf9\u7c7b\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u6240\u793a\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MemoryBlock&amp; operator=(MemoryBlock&amp;&amp; other)\n{\n}<\/code><\/pre>\n\n\n\n<p> \u5728\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e2d\uff0c\u5982\u679c\u5c1d\u8bd5\u5c06\u5bf9\u8c61\u8d4b\u7ed9\u81ea\u8eab\uff0c\u5219\u6dfb\u52a0\u4e0d\u6267\u884c\u8fd0\u7b97\u7684\u6761\u4ef6\u8bed\u53e5\u3002 <\/p>\n\n\n\n<p> if (this != &amp;other) { } <\/p>\n\n\n\n<p> \u5728\u6761\u4ef6\u8bed\u53e5\u4e2d\uff0c\u4ece\u8981\u5c06\u5176\u8d4b\u503c\u7684\u5bf9\u8c61\u4e2d\u91ca\u653e\u6240\u6709\u8d44\u6e90\uff08\u5982\u5185\u5b58\uff09\u3002 <\/p>\n\n\n\n<p> \u4ee5\u4e0b\u793a\u4f8b\u4ece\u8981\u5c06\u5176\u8d4b\u503c\u7684\u5bf9\u8c61\u4e2d\u91ca\u653e\u00a0<code>_data<\/code>\u00a0\u6210\u5458\uff1a  delete[] _data; <\/p>\n\n\n\n<p> \u6267\u884c\u7b2c\u4e00\u4e2a\u8fc7\u7a0b\u4e2d\u7684\u6b65\u9aa4 2 \u548c\u6b65\u9aa4 3 \u4ee5\u5c06\u6570\u636e\u6210\u5458\u4ece\u6e90\u5bf9\u8c61\u8f6c\u79fb\u5230\u8981\u6784\u9020\u7684\u5bf9\u8c61\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Copy the data pointer and its length from the\n\/\/ source object.\n_data = other._data;\n_length = other._length;\n\n\/\/ Release the data pointer from the source object so that\n\/\/ the destructor does not free the memory multiple times.\nother._data = nullptr;\nother._length = 0;<\/code><\/pre>\n\n\n\n<p> \u8fd4\u56de\u5bf9\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u6240\u793a\uff1a  return *this; <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-complete-move-constructor-and-assignment-operator\">\u793a\u4f8b\uff1a\u5b8c\u6210\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26<\/h2>\n\n\n\n<p><code>MemoryBlock<\/code>\u00a0\u7c7b\u7684\u5b8c\u6574\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Move constructor.\nMemoryBlock(MemoryBlock&amp;&amp; other) noexcept\n   : _data(nullptr)\n   , _length(0)\n{\n   std::cout &lt;&lt; \"In MemoryBlock(MemoryBlock&amp;&amp;). length = \"\n             &lt;&lt; other._length &lt;&lt; \". Moving resource.\" &lt;&lt; std::endl;\n\n   \/\/ Copy the data pointer and its length from the\n   \/\/ source object.\n   _data = other._data;\n   _length = other._length;\n\n   \/\/ Release the data pointer from the source object so that\n   \/\/ the destructor does not free the memory multiple times.\n   other._data = nullptr;\n   other._length = 0;\n}\n\n\/\/ Move assignment operator.\nMemoryBlock&amp; operator=(MemoryBlock&amp;&amp; other) noexcept\n{\n   std::cout &lt;&lt; \"In operator=(MemoryBlock&amp;&amp;). length = \"\n             &lt;&lt; other._length &lt;&lt; \".\" &lt;&lt; std::endl;\n\n   if (this != &amp;other)\n   {\n      \/\/ Free the existing resource.\n      delete[] _data;\n\n      \/\/ Copy the data pointer and its length from the\n      \/\/ source object.\n      _data = other._data;\n      _length = other._length;\n\n      \/\/ Release the data pointer from the source object so that\n      \/\/ the destructor does not free the memory multiple times.\n      other._data = nullptr;\n      other._length = 0;\n   }\n   return *this;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-use-move-semantics-to-improve-performance\">\u793a\u4f8b\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u63d0\u9ad8\u6027\u80fd<\/h2>\n\n\n\n<p> \u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u79fb\u52a8\u8bed\u4e49\u5982\u4f55\u80fd\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u00a0\u6b64\u793a\u4f8b\u5c06\u4e24\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u4e00\u4e2a\u77e2\u91cf\u5bf9\u8c61\uff0c\u7136\u540e\u5728\u4e24\u4e2a\u73b0\u6709\u5143\u7d20\u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u5143\u7d20\u3002\u00a0<code>vector<\/code>\u7c7b\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\uff0c\u901a\u8fc7\u79fb\u52a8\u77e2\u91cf\u7684\u5143\u7d20\u800c\u4e0d\u662f\u590d\u5236\u77e2\u91cf\u6765\u6709\u6548\u5730\u6267\u884c\u63d2\u5165\u64cd\u4f5c\u3002 <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ rvalue-references-move-semantics.cpp\n\/\/ compile with: \/EHsc\n#include \"MemoryBlock.h\"\n#include &lt;vector>\n\nusing namespace std;\n\nint main()\n{\n   \/\/ Create a vector object and add a few elements to it.\n   vector&lt;MemoryBlock> v;\n   v.push_back(MemoryBlock(25));\n   v.push_back(MemoryBlock(75));\n\n   \/\/ Insert a new element into the second position of the vector.\n   v.insert(v.begin() + 1, MemoryBlock(50));\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>In MemoryBlock(size_t). length = 25.\nIn MemoryBlock(MemoryBlock&amp;&amp;). length = 25. Moving resource.\nIn ~MemoryBlock(). length = 0.\nIn MemoryBlock(size_t). length = 75.\nIn MemoryBlock(MemoryBlock&amp;&amp;). length = 75. Moving resource.\nIn MemoryBlock(MemoryBlock&amp;&amp;). length = 25. Moving resource.\nIn ~MemoryBlock(). length = 0.\nIn ~MemoryBlock(). length = 0.\nIn MemoryBlock(size_t). length = 50.\nIn MemoryBlock(MemoryBlock&amp;&amp;). length = 50. Moving resource.\nIn MemoryBlock(MemoryBlock&amp;&amp;). length = 25. Moving resource.\nIn MemoryBlock(MemoryBlock&amp;&amp;). length = 75. Moving resource.\nIn ~MemoryBlock(). length = 0.\nIn ~MemoryBlock(). length = 0.\nIn ~MemoryBlock(). length = 0.\nIn ~MemoryBlock(). length = 25. Deleting resource.\nIn ~MemoryBlock(). length = 50. Deleting resource.\nIn ~MemoryBlock(). length = 75. Deleting resource.\n\n\u5728 Visual Studio 2010 \u4e4b\u524d\uff0c\u6b64\u793a\u4f8b\u751f\u6210\u4ee5\u4e0b\u8f93\u51fa\uff1a\nIn MemoryBlock(size_t). length = 25.\nIn MemoryBlock(const MemoryBlock&amp;). length = 25. Copying resource.\nIn ~MemoryBlock(). length = 25. Deleting resource.\nIn MemoryBlock(size_t). length = 75.\nIn MemoryBlock(const MemoryBlock&amp;). length = 25. Copying resource.\nIn ~MemoryBlock(). length = 25. Deleting resource.\nIn MemoryBlock(const MemoryBlock&amp;). length = 75. Copying resource.\nIn ~MemoryBlock(). length = 75. Deleting resource.\nIn MemoryBlock(size_t). length = 50.\nIn MemoryBlock(const MemoryBlock&amp;). length = 50. Copying resource.\nIn MemoryBlock(const MemoryBlock&amp;). length = 50. Copying resource.\nIn operator=(const MemoryBlock&amp;). length = 75. Copying resource.\nIn operator=(const MemoryBlock&amp;). length = 50. Copying resource.\nIn ~MemoryBlock(). length = 50. Deleting resource.\nIn ~MemoryBlock(). length = 50. Deleting resource.\nIn ~MemoryBlock(). length = 25. Deleting resource.\nIn ~MemoryBlock(). length = 50. Deleting resource.\nIn ~MemoryBlock(). length = 75. Deleting resource.<\/code><\/pre>\n\n\n\n<p> \u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u7684\u6b64\u793a\u4f8b\u7248\u672c\u6bd4\u4e0d\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u7684\u7248\u672c\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u524d\u8005\u6267\u884c\u7684\u590d\u5236\u3001\u5185\u5b58\u5206\u914d\u548c\u5185\u5b58\u91ca\u653e\u64cd\u4f5c\u66f4\u5c11\u3002 <\/p>\n\n\n\n<p><a href=\"https:\/\/docs.microsoft.com\/zh-cn\/cpp\/standard-library\/utility-functions?view=msvc-160#move\">std::move<\/a>\u51fd\u6570\u5c06 lvalue \u8f6c\u6362\u00a0<code>other<\/code>\u00a0\u4e3a\u53f3\u503c\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a C++ \u521b\u5efa\u79fb\u52a8\u6784\u9020\u51fd\u6570 \u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684\u6784\u9020\u51fd\u6570\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u91c7\u7528\u4e00\u4e2a\u5bf9\u7c7b\u7c7b\u578b\u7684\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u53c2\u6570\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u6240 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2967"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2967"}],"version-history":[{"count":1,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2967\/revisions"}],"predecessor-version":[{"id":2968,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2967\/revisions\/2968"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}