{"id":1217,"date":"2014-02-21T13:21:27","date_gmt":"2014-02-21T05:21:27","guid":{"rendered":"http:\/\/www.dogeye.net\/?p=1217"},"modified":"2014-02-21T13:21:27","modified_gmt":"2014-02-21T05:21:27","slug":"%e5%8f%b3%e5%80%bc%e5%bc%95%e7%94%a8%e4%b8%8d%e6%98%af%e5%8f%b3%e5%80%bc%e8%80%8c%e6%98%af%e5%b7%a6%e5%80%bc","status":"publish","type":"post","link":"https:\/\/ykyi.net\/?p=1217","title":{"rendered":"&#8220;\u53f3\u503c\u5f15\u7528&#8221;\u4e0d\u662f&#8221;\u53f3\u503c&#8221;,\u800c\u662f&#8221;\u5de6\u503c&#8221;"},"content":{"rendered":"<p><span style=\"font-size:14px;\">\u4eae\u778e\u4f60\u7684\u72d7\u773c\u4e86\u5427~\u8fd9\u4e48\u62d7\u53e3\u3002 \uff01\u6765\u8865\u4e00\u4e0bC++\u57fa\u7840\u5427~~<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u53f3\u503c\u5f15\u7528 rvalue reference<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u53f3\u503c rvalue<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u5de6\u503c lvalue<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u4ec0\u4e48\u662f\u5de6\u503c lvalue\u5462\u3002<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u672c\u8d28\u4e0a\uff0c\u5de6\u503c\u662f\u4e00\u4e2a\u53ef\u4ee5\u62ff\u5230\u5176\u5730\u5740\u7684\u8868\u8fbe\u5f0f\u3002\u5de6\u503clvalue\u91cc\u7684l,\u672c\u8d28\u4e0a\u662flocatable\u7684\u7f29\u5199\uff0c\u56e0\u4e3alvalue\u901a\u5e38\u53ef\u4ee5\u653e\u7f6e\u4e8e\u8868\u8fbe\u5f0f\u7684\u5de6\u8fb9\uff0c\u4e8e\u662f\u5c31\u6709\u4e86\u5de6left\u503c\u7684\u8bf4\u6cd5\u3002<\/span><\/p>\n<p><span style=\"font-size:14px;\">In C++, an lvalue is an expression whose address can be taken, essentially it&#39;s a locator value.<\/span><\/p>\n<pre class=\"brush:cpp\">class MetaData\n{\npublic:\n\tMetaData (int size, const std::string&amp; name)\n\t\t: _name( name )\n\t\t, _size( size )\n\t{}\n\n\t\/\/ copy constructor\n\tMetaData (const MetaData&amp; other)\n\t\t: _name( other._name )\n\t\t, _size( other._size )\n\t{\n\t\tcout &lt;&lt; &quot;copy\\n&quot;;\n\t}\n\n\t\/\/ move constructor\n\tMetaData (MetaData&amp;&amp; other)\n\t\t: _name( other._name )\n\t\t, _size( other._size )\n\t{\n\t\tcout &lt;&lt; &quot;move\\n&quot;;\n\t}\n\n\tstd::string getName () const { return _name; }\n\tint getSize () const { return _size; }\nprivate:\n\tstd::string _name;\n\tint _size;\n};\n\nclass ArrayWrapper\n{\npublic:\n\t\/\/ default constructor produces a moderately sized array\n\tArrayWrapper ()\n\t\t: _p_vals( new int[ 64 ] )\n\t\t, _metadata( 64, &quot;ArrayWrapper&quot; )\n\t{}\n\n\tArrayWrapper (int n)\n\t\t: _p_vals( new int[ n ] )\n\t\t, _metadata( n, &quot;ArrayWrapper&quot; )\n\t{}\n\n\t\/\/ move constructor\n\tArrayWrapper (ArrayWrapper&amp;&amp; other)\n\t\t: _p_vals( other._p_vals  )\n\t\t, _metadata( move(other._metadata ))\n\t{\n\t\tother._p_vals = NULL;\n\t}\n\n\t\/\/ copy constructor\n\tArrayWrapper (const ArrayWrapper&amp; other)\n\t\t: _p_vals( new int[ other._metadata.getSize() ] )\n\t\t, _metadata( other._metadata )\n\t{\n\t\tfor ( int i = 0; i &lt; _metadata.getSize(); ++i )\n\t\t{\n\t\t\t_p_vals[ i ] = other._p_vals[ i ];\n\t\t}\n\t}\n\t~ArrayWrapper ()\n\t{\n\t\tdelete [] _p_vals;\n\t}\nprivate:\n\tint *_p_vals;\n\tMetaData _metadata;\n};\n\nArrayWrapper get()\n{\n\tArrayWrapper aw;\n\treturn aw;\n}\n\nint main()\n{\n\tArrayWrapper aw(get());\n\treturn 0;\n}<\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u7684ArrayWrapper\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u80fd\u5de5\u4f5c\u5417\uff1f<\/p>\n<p>\u9519\uff01\u7b54\u6848\u662f\u4e0d\u80fd~ \u66f4\u8be6\u7ec6\uff0c\u8bf7\u53c2\u8003\uff1a<a href=\"http:\/\/www.cprogramming.com\/c++11\/rvalue-references-and-move-semantics-in-c++11.html\">http:\/\/www.cprogramming.com\/c++11\/rvalue-references-and-move-semantics-in-c++11.html<\/a><\/p>\n<p>\u6982\u62ec\u5730\u8bb2\uff0c\u672c\u8d28\u539f\u56e0\u662f\u56e0\u4e3a\uff1a\u53f3\u503c\u5f15\u7528\u4e0d\u662f\u53f3\u503c\uff0c\u800c\u662f\u5de6\u503c\u3002\u6240\u4ee5ArrayWrapper\u91cc\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4e0d\u4f1a\u8c03\u7528ArrayWrapper\u5185\u5d4c\u7684MetaData\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0c\u800c\u662f\u8c03\u7528\u5b83MetaData\u7684\u590d\u5236\u6784\u9020\u51fd\u6570\u3002\u4e3a\u4e86\u5f97\u5230\u671f\u671b\u7684\u884c\u4e3a\uff0c\u9700\u8981\u4f7f\u7528std::move\u628alvalue\u8f6c\u6362\u6210rvalue.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eae\u778e\u4f60\u7684\u72d7\u773c\u4e86\u5427~\u8fd9\u4e48\u62d7\u53e3\u3002 \uff01\u6765\u8865\u4e00\u4e0bC++\u57fa\u7840\u5427~~ \u53f3\u503c\u5f15\u7528 rvalue reference \u53f3\u503c rvalue \u5de6\u503c lvalue \u4ec0\u4e48\u662f\u5de6\u503c lvalue\u5462\u3002 \u672c\u8d28\u4e0a\uff0c\u5de6\u503c\u662f\u4e00\u4e2a\u53ef\u4ee5\u62ff\u5230\u5176\u5730\u5740\u7684\u8868\u8fbe\u5f0f\u3002\u5de6\u503clvalue\u91cc\u7684l,\u672c\u8d28\u4e0a\u662flocatable\u7684\u7f29\u5199\uff0c\u56e0\u4e3alvalue\u901a\u5e38\u53ef\u4ee5\u653e\u7f6e\u4e8e\u8868\u8fbe\u5f0f\u7684\u5de6\u8fb9\uff0c\u4e8e\u662f\u5c31\u6709\u4e86\u5de6left\u503c\u7684\u8bf4\u6cd5\u3002 In C++, an lvalue is an expression whose address can be taken, essentially it&#39;s a locator value. class MetaData { public: MetaData (int size, const std::string&amp; name) : _name( name ) , _size( size ) {} \/\/ copy constructor MetaData (const MetaData&amp; other) &hellip; <a href=\"https:\/\/ykyi.net\/?p=1217\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;&#8220;\u53f3\u503c\u5f15\u7528&#8221;\u4e0d\u662f&#8221;\u53f3\u503c&#8221;,\u800c\u662f&#8221;\u5de6\u503c&#8221;&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[20],"class_list":["post-1217","post","type-post","status-publish","format-standard","hentry","category-tech_articles","tag-c"],"_links":{"self":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1217"}],"version-history":[{"count":0,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1217\/revisions"}],"wp:attachment":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}