{"id":1523,"date":"2026-06-04T22:40:22","date_gmt":"2026-06-04T14:40:22","guid":{"rendered":"https:\/\/cqfdreamgarden.art\/?p=1523"},"modified":"2026-06-04T22:47:35","modified_gmt":"2026-06-04T14:47:35","slug":"%e5%85%b3%e4%ba%8ecpp%e5%bc%95%e7%94%a8%e7%9a%84%e6%80%9d%e8%80%83","status":"publish","type":"post","link":"https:\/\/cqfdreamgarden.art\/index.php\/2026\/06\/04\/%e5%85%b3%e4%ba%8ecpp%e5%bc%95%e7%94%a8%e7%9a%84%e6%80%9d%e8%80%83\/","title":{"rendered":"\u5173\u4e8eC++\u5f15\u7528\u7684\u601d\u8003"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 C++ \u91cc\uff0c\u4ec0\u4e48\u65f6\u5019\u52a0\u5f15\u7528\u3001\u4ec0\u4e48\u65f6\u5019\u4e0d\u52a0\u5f15\u7528\uff0c\u6838\u5fc3\u53d6\u51b3\u4e8e\u4e09\u4e2a\u56e0\u7d20\uff1a<strong>\u751f\u547d\u5468\u671f<\/strong>\u3001<strong>\u4fee\u6539\u9700\u6c42<\/strong>\u548c<strong>\u62f7\u8d1d\u6210\u672c<\/strong>\u3002\u4e0b\u9762\u4ece\u8fd4\u56de\u503c\u3001\u4f20\u5165\u53c2\u6570\u3001\u4ee5\u53ca\u201c\u52a0\u4e0d\u52a0\u90fd\u65e0\u6240\u8c13\u201d\u7684\u573a\u666f\u6df1\u5165\u5206\u6790\uff0c\u5e76\u7ed9\u51fa\u5927\u91cf\u4f8b\u5b50\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u8fd4\u56de\u503c\uff1a\u4ec0\u4e48\u65f6\u5019\u52a0&nbsp;<code>&amp;<\/code>&nbsp;\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 \u5e94\u8be5\u8fd4\u56de\u5f15\u7528\uff08<code>T&amp;<\/code>&nbsp;\u6216&nbsp;<code>const T&amp;<\/code>\uff09\u7684\u573a\u666f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u524d\u63d0\uff1a\u8fd4\u56de\u7684\u5bf9\u8c61\u5728\u51fd\u6570\u7ed3\u675f\u540e\u4ecd\u7136\u5b58\u5728\uff0c\u4e14\u751f\u547d\u5468\u671f\u7531\u8c03\u7528\u65b9\u53ef\u63a7\u3002<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u573a\u666f<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u5178\u578b\u4f8b\u5b50<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u539f\u56e0<\/th><\/tr><\/thead><tbody><tr><td>\u8fd4\u56de\u7c7b\u6210\u5458<\/td><td><code>const std::string&amp; getName() const<\/code><\/td><td>\u6210\u5458\u751f\u547d\u5468\u671f\u4e0e\u5bf9\u8c61\u4e00\u81f4\uff0c\u907f\u514d\u62f7\u8d1d\u5927\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>\u64cd\u4f5c\u7b26\u91cd\u8f7d\u9700\u8981\u5de6\u503c<\/td><td><code>T&amp; operator[]<\/code><\/td><td>\u5141\u8bb8&nbsp;<code>vec[i] = x<\/code>&nbsp;\u4fee\u6539\u5143\u7d20<\/td><\/tr><tr><td>\u94fe\u5f0f\u8c03\u7528<\/td><td><code>MyClass&amp; operator=(const MyClass&amp;)<\/code>&nbsp;\u8fd4\u56de&nbsp;<code>*this<\/code><\/td><td>\u652f\u6301&nbsp;<code>a = b = c<\/code><\/td><\/tr><tr><td>\u9759\u6001\u5c40\u90e8\u53d8\u91cf \/ \u5168\u5c40\u53d8\u91cf<\/td><td><code>const Config&amp; getConfig()<\/code><\/td><td>\u5168\u5c40\/\u9759\u6001\u5bf9\u8c61\u6c38\u8fdc\u5b58\u5728<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Person {\n    std::string name_;\npublic:\n    <em>\/\/ \u8fd4\u56de const \u5f15\u7528\uff1a\u53ea\u8bfb\u8bbf\u95ee\uff0c\u4e0d\u62f7\u8d1d<\/em>\n    const std::string&amp; getName() const { return name_; }\n\n    <em>\/\/ \u8fd4\u56de\u53ef\u4fee\u6539\u5f15\u7528\uff1a\u5141\u8bb8\u5916\u90e8\u76f4\u63a5\u4fee\u6539<\/em>\n    std::string&amp; getName() { return name_; }\n};\n\n<em>\/\/ \u64cd\u4f5c\u7b26[]<\/em>\nint&amp; operator[](std::vector&lt;int&gt;&amp; v, size_t i) {\n    return v.data()[i]; <em>\/\/ \u8fd4\u56de\u5f15\u7528\uff0c\u53ef\u8d4b\u503c<\/em>\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u26a0\ufe0f&nbsp;<strong>\u6ce8\u610f<\/strong>\uff1a\u5982\u679c\u8fd4\u56de\u6210\u5458\u7684\u975e\u5e38\u91cf\u5f15\u7528\u4f1a\u7834\u574f\u5c01\u88c5\u6027\uff0c\u9700\u8981\u6743\u8861\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 \u7edd\u5bf9\u4e0d\u80fd\u8fd4\u56de\u5f15\u7528\u7684\u573a\u666f\uff08\u5fc5\u987b\u6309\u503c\u8fd4\u56de\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4efb\u4f55\u51fd\u6570\u5185\u90e8\u521b\u5efa\u7684\u5c40\u90e8\u5bf9\u8c61\uff08\u81ea\u52a8\u5b58\u50a8\u671f\uff09\u90fd\u4e0d\u80fd\u8fd4\u56de\u5f15\u7528<\/strong>\uff0c\u5426\u5219\u4ea7\u751f<strong>\u60ac\u5782\u5f15\u7528<\/strong>\uff08dangling reference\uff09\uff0c\u5c5e\u4e8e\u672a\u5b9a\u4e49\u884c\u4e3a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/\/ \u274c \u81f4\u547d\u9519\u8bef<\/em>\nconst std::string&amp; createString() {\n    std::string local = \"hello\";\n    return local; <em>\/\/ \u51fd\u6570\u7ed3\u675f\u65f6 local \u5df2\u9500\u6bc1<\/em>\n}\n\n<em>\/\/ \u2705 \u6b63\u786e\uff1a\u6309\u503c\u8fd4\u56de\uff08\u4f9d\u9760 RVO\/\u79fb\u52a8\u8bed\u4e49\u4f18\u5316\uff09<\/em>\nstd::string createString() {\n    std::string local = \"hello\";\n    return local; <em>\/\/ \u76f4\u63a5\u6784\u9020\u5728\u8c03\u7528\u65b9\uff0c\u96f6\u62f7\u8d1d<\/em>\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5373\u4fbf\u8fd4\u56de\u5c40\u90e8\u5bf9\u8c61\u7684 const \u5f15\u7528\uff0c\u4e5f\u4e0d\u5b89\u5168\u3002\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u8b66\u544a\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u4f20\u5165\u53c2\u6570\uff1a\u4ec0\u4e48\u65f6\u5019\u52a0&nbsp;<code>&amp;<\/code>&nbsp;\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 \u9700\u8981\u4fee\u6539\u5b9e\u53c2 \u2192&nbsp;<code>T&amp;<\/code>\uff08\u975e\u5e38\u91cf\u5f15\u7528\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void swap(int&amp; a, int&amp; b) {\n    int tmp = a;\n    a = b;\n    b = tmp;\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 \u53ea\u8bfb\u5927\u5bf9\u8c61\uff0c\u907f\u514d\u62f7\u8d1d \u2192&nbsp;<code>const T&amp;<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/\/ \u5047\u8bbe LargeData \u62f7\u8d1d\u6210\u672c\u5f88\u9ad8<\/em>\nvoid process(const LargeData&amp; data) {\n    <em>\/\/ \u53ea\u8bfb data\uff0c\u65e0\u62f7\u8d1d<\/em>\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u6807\u51c6\u5e93\u5bb9\u5668\uff08<code>std::string<\/code>,&nbsp;<code>std::vector<\/code>&nbsp;\u7b49\uff09\u548c\u81ea\u5b9a\u4e49\u5927\u5bf9\u8c61\uff0c\u8fd9\u662f\u9996\u9009\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.3 \u5c0f\u5bf9\u8c61\/\u57fa\u672c\u7c7b\u578b \u2192 \u6309\u503c\u4f20\u9012&nbsp;<code>T<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void foo(int x);       <em>\/\/ \u597d<\/em>\nvoid bar(double d);    <em>\/\/ \u597d<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u539f\u56e0\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>int<\/code>\/<code>double<\/code>\u00a0\u4e00\u822c\u5927\u5c0f \u2264 \u4e24\u4e2a\u6307\u9488\uff0c\u62f7\u8d1d\u6781\u5feb\u3002<\/li>\n\n\n\n<li>\u5f15\u7528\u5728\u5e95\u5c42\u5b9e\u73b0\u4e3a\u6307\u9488\uff0c\u4f1a\u5f15\u5165\u4e00\u6b21\u95f4\u63a5\u8bbf\u95ee\uff08\u89e3\u5f15\u7528\uff09\uff0c\u53ef\u80fd\u6bd4\u62f7\u8d1d\u66f4\u6162\u3002<\/li>\n\n\n\n<li>\u6309\u503c\u4f20\u53ef\u4ee5\u4eab\u53d7\u5bc4\u5b58\u5668\u4f20\u53c2\u4f18\u5316\uff0c\u5e38\u91cf\u5f15\u7528\u901a\u5e38\u8981\u8d70\u5185\u5b58\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2.4 \u51fd\u6570\u5185\u65e0\u8bba\u5982\u4f55\u9700\u8981\u526f\u672c \u2192 \u6309\u503c\u4f20\u9012 +&nbsp;<code>std::move<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Holder {\n    std::string name_;\npublic:\n    <em>\/\/ \u6309\u503c\u4f20\u9012\uff0c\u5185\u90e8\u79fb\u52a8<\/em>\n    void setName(std::string name) {\n        name_ = std::move(name);\n    }\n};\n\n<em>\/\/ \u4f7f\u7528\uff1a<\/em>\nholder.setName(\"hello\");        <em>\/\/ \u4e34\u65f6\u5bf9\u8c61\uff0c\u79fb\u52a8\u6784\u9020\u5230 name<\/em>\nstd::string s = \"world\";\nholder.setName(s);              <em>\/\/ \u5de6\u503c\u62f7\u8d1d\u4e00\u6b21\uff0c\u7136\u540e\u79fb\u52a8<\/em>\nholder.setName(std::move(s));   <em>\/\/ \u53f3\u503c\uff0c\u4e24\u6b21\u79fb\u52a8\uff08\u53ef\u63a5\u53d7\uff09<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u5f0f\u5728 C++11 \u540e\u5f88\u5e38\u89c1\uff0c\u517c\u987e\u5de6\u503c\u548c\u53f3\u503c\uff0c\u51cf\u5c11\u91cd\u8f7d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5230\u5e95\u6709\u6ca1\u6709\u201c\u52a0\u4e0d\u52a0\u5f15\u7528\u90fd\u65e0\u6240\u8c13\u201d\u7684\u60c5\u51b5\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6709\u3002<\/strong>&nbsp;\u8fd9\u7c7b\u60c5\u51b5\u901a\u5e38\u53d1\u751f\u5728<strong>\u5c0f\u5bf9\u8c61\u3001\u53ea\u8bfb\u3001\u4e14\u4e0d\u6d89\u53ca\u751f\u547d\u5468\u671f\u95ee\u9898<\/strong>\u7684\u4e0a\u4e0b\u6587\u4e2d\uff0c\u4e3b\u8981\u8868\u73b0\u4e3a\u4e24\u7c7b\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u53c2\u6570\uff1a\u5c0f\u57fa\u672c\u7c7b\u578b\u6216\u5c0f\u578b POD\uff0c<code>T<\/code>&nbsp;vs&nbsp;<code>const T&amp;<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void print(int x);          <em>\/\/ \u65b9\u5f0f A<\/em>\nvoid print(const int&amp; x);   <em>\/\/ \u65b9\u5f0f B<\/em><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bed\u4e49\u4e0a\uff0c\u4e24\u8005\u90fd\u4e0d\u4f1a\u4fee\u6539\u5b9e\u53c2\u3002<\/li>\n\n\n\n<li>\u6027\u80fd\u4e0a\uff0c<code>int<\/code>\u00a0\u6309\u503c\u4f20\u901a\u5e38\u662f\u5bc4\u5b58\u5668\u76f4\u63a5\u4f20\u503c\uff1b<code>const int&amp;<\/code>\u00a0\u4f1a\u4f20\u5730\u5740\u5e76\u89e3\u5f15\u7528\uff0c\u53cd\u800c\u66f4\u6162\u3002<\/li>\n\n\n\n<li>\u56e0\u6b64\u4e3b\u6d41\u91cf\u4ee3\u7801\u89c4\u8303\uff08\u5982 Google C++ Style Guide\uff09\u660e\u786e\u8981\u6c42\uff1a<strong>\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u76f4\u63a5\u7528\u503c\u4f20\u9012<\/strong>\uff0c\u4e0d\u8981\u7528\u5f15\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u201c\u52a0\u4e0d\u52a0\u5f15\u7528\u90fd\u65e0\u6240\u8c13\u201d\u4ec5\u4ec5\u662f\u8bf4\u201c\u90fd\u80fd\u6b63\u786e\u8fd0\u884c\uff0c\u4e0d\u4f1a\u51fa\u9519\u201d\uff0c\u4f46\u6309\u503c\u4f20\u9012\u66f4\u4f18\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u5927\u5c0f\u4e3a 8 \u5b57\u8282\u4ee5\u5185\u3001\u5e73\u51e1\u53ef\u62f7\u8d1d\u7684\u7c7b\u578b\uff08\u5982&nbsp;<code>std::pair&lt;int,int&gt;<\/code>&nbsp;\u6216\u7b80\u5355\u7684\u7ed3\u6784\u4f53\uff09\uff0c\u6309\u503c\u548c&nbsp;<code>const&amp;<\/code>&nbsp;\u5728\u4f18\u5316\u5168\u5f00\u65f6\u53ef\u80fd\u6027\u80fd\u76f8\u540c\uff0c\u4f46\u503c\u4f20\u9012\u8bed\u4e49\u66f4\u6e05\u6670\uff0c\u907f\u514d\u95f4\u63a5\u5f00\u9500\u731c\u6d4b\uff0c\u4ecd\u662f\u63a8\u8350\u505a\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">struct Point { int x, y; };\nvoid draw(Point p);            <em>\/\/ \u53ef\u4ee5\uff0c\u62f7\u8d1d\u4e24\u4e2a int \u5f88\u8f7b\u91cf<\/em>\nvoid draw(const Point&amp; p);     <em>\/\/ \u4e5f\u53ef\u4ee5\uff0c\u7a0d\u591a\u4f59<\/em><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 \u8fd4\u56de\u503c\uff1a\u6781\u5c0f\u7684\u5bf9\u8c61\uff0c\u4e14\u672c\u6765\u5c31\u5728\u5916\u90e8\u5b58\u5728<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8003\u8651\u4e00\u4e2a\u8fd4\u56de\u201c\u5e38\u91cf\u201d\u6216\u5168\u5c40\u72b6\u6001\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/\/ \u65b9\u5f0f1\uff1a\u6309\u503c\u8fd4\u56de<\/em>\nint getAnswer() { return 42; }\n\n<em>\/\/ \u65b9\u5f0f2\uff1a\u8fd4\u56de const \u5f15\u7528\uff08\u5047\u8bbe\u5e38\u91cf\u5728\u522b\u5904\u5b9a\u4e49\uff09<\/em>\nconst int&amp; getAnswer() {\n    static const int answer = 42;\n    return answer;\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u00a0<code>int<\/code>\u00a0\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de\u00a0<code>42<\/code>\u00a0\u66f4\u597d\uff0c\u7f16\u8bd1\u5668\u76f4\u63a5\u4f18\u5316\u4e3a\u5e38\u6570\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8fd4\u56de\u67d0\u4e2a\u9884\u5b9a\u4e49\u9759\u6001\u00a0<code>std::string<\/code>\uff0c\u7528\u00a0<code>const std::string&amp;<\/code>\u00a0\u66f4\u597d\u907f\u514d\u62f7\u8d1d\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u5982\u679c\u672c\u6765\u5c31\u662f&nbsp;<code>int<\/code>&nbsp;\u7c7b\u578b\uff0c\u6309\u503c\u8fd4\u56de\u4e0e\u8fd4\u56de const \u5f15\u7528\u76f8\u6bd4\uff0c<strong>\u66f4\u5b89\u5168\u3001\u66f4\u7b80\u5355\uff0c\u4e14\u6ca1\u6709\u6027\u80fd\u52a3\u52bf<\/strong>\u3002\u8fd9\u79cd\u573a\u5408\u53ef\u4ee5\u8ba4\u4e3a\u201c\u65e0\u6240\u8c13\uff0c\u4f46\u5f3a\u70c8\u5efa\u8bae\u6309\u503c\u201d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u603b\u7ed3\uff1a\u4ec0\u4e48\u65f6\u5019\u52a0\u5f15\u7528\uff1f\u51b3\u7b56\u6307\u5357<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fd4\u56de\u503c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">text<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u51fd\u6570\u4f53\u5185\u521b\u5efa\u7684\u65b0\u5bf9\u8c61  \u2192  \u6309\u503c\u8fd4\u56de\uff08\u4f9d\u8d56 RVO\/\u79fb\u52a8\uff09\n\u8fd4\u56de\u73b0\u6709\u5bf9\u8c61\uff08\u6210\u5458\/\u9759\u6001\/\u5168\u5c40\uff09\n    \u251c\u2500 \u9700\u8981\u5141\u8bb8\u4fee\u6539 \u2192 T&amp;\n    \u251c\u2500 \u53ea\u8bfb\u8bbf\u95ee     \u2192 const T&amp;\n    \u2514\u2500 \u5c0f\u5bf9\u8c61\/\u57fa\u672c\u7c7b\u578b  \u2192  \u4e5f\u53ef\u6309\u503c\u8fd4\u56de\uff08\u66f4\u7b80\u5355\uff09<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f20\u5165\u53c2\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">text<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u662f\u5426\u9700\u8981\u4fee\u6539\u5b9e\u53c2\uff1f\n    \u251c\u2500 \u662f \u2192 T&amp;\n    \u2514\u2500 \u5426 \u2192 \u5bf9\u8c61\u5927\u5c0f\u548c\u62f7\u8d1d\u6210\u672c\u5982\u4f55\uff1f\n              \u251c\u2500 \u5927\u5bf9\u8c61\/\u975e\u57fa\u672c\u7c7b\u578b \u2192 const T&amp;\n              \u251c\u2500 \u57fa\u672c\u7c7b\u578b\/\u5c0fPOD   \u2192 T\uff08\u503c\u4f20\u9012\uff09\n              \u2514\u2500 \u51fd\u6570\u5185\u5fc5\u987b\u62f7\u8d1d   \u2192 \u8003\u8651 \u6309\u503c\u4f20\u9012 + std::move<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u52a0\u4e0d\u52a0\u5f15\u7528\u90fd\u65e0\u6240\u8c13\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53c2\u6570<\/strong>\uff1a\u57fa\u672c\u7c7b\u578b\u6216\u5c0f\u7ed3\u6784\u4f53\uff0c\u65e2\u53ef\u4ee5\u7528\u00a0<code>T<\/code>\u00a0\u4e5f\u53ef\u4ee5\u7528\u00a0<code>const T&amp;<\/code>\uff0c\u4f46\u00a0<code>T<\/code>\u00a0\u901a\u5e38\u66f4\u4f18\uff0c\u907f\u514d\u95f4\u63a5\u5f15\u7528\u5f00\u9500\u3002<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u503c<\/strong>\uff1a\u57fa\u672c\u7c7b\u578b\u76f4\u63a5\u6309\u503c\u8fd4\u56de\u6700\u7b80\u5355\u5b89\u5168\uff1b\u8fd4\u56de\u5168\u5c40\/\u9759\u6001\u57fa\u672c\u7c7b\u578b\u7528\u5f15\u7528\u53ef\u80fd\u7565\u6709\u6536\u76ca\uff0c\u4f46\u901a\u5e38\u6ca1\u592a\u5927\u5fc5\u8981\u589e\u52a0\u590d\u6742\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u6838\u5fc3\u539f\u5219<\/strong>\uff1a\u5982\u679c\u4e24\u79cd\u65b9\u5f0f\u5728\u8bed\u4e49\u4e0a\u90fd\u6b63\u786e\uff0c\u5c31\u9009<strong>\u66f4\u7b80\u5355\u3001\u62f7\u8d1d\u6210\u672c\u66f4\u4f4e\u3001\u66f4\u4e0d\u6613\u51fa\u9519<\/strong>\u7684\u90a3\u4e2a\u2014\u2014\u5c0f\u5bf9\u8c61\u503e\u5411\u4e8e\u503c\u4f20\u9012\uff0c\u5927\u5bf9\u8c61\u503e\u5411\u4e8e const \u5f15\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u638c\u63e1\u8fd9\u4e9b\uff0c\u5e94\u5bf9 90% \u7684 C++ \u51fd\u6570\u8bbe\u8ba1\u573a\u666f\u5c31\u8db3\u591f\u4e86\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-8d07c1d69b1a871aa98b73dac0aa74a1 wp-block-paragraph\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56f0\u60d1\u89e3\u8bfb<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4e3a\u4ec0\u4e48\u8fd4\u56de\u503c\u8981&nbsp;<code>const T&amp;<\/code>\uff0c\u800c\u4e0d\u662f\u4e0d\u8981\u5f15\u7528\u3001\u4e0d\u52a0&nbsp;<code>const<\/code>\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u7684\u5efa\u8bae\u662f\uff1a\u201c\u53ea\u8bfb\u7684\u8bdd\uff0c\u4e0d\u52a0\u5f15\u7528\u4e5f\u4e0d\u52a0 const \u5c31\u884c\u4e86\u201d\u3002\u8fd9\u5176\u5b9e\u5bf9\u5e94<strong>\u6309\u503c\u8fd4\u56de<\/strong>\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">std::string getName() {\n    return name_;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u786e\u5b9e\u5b89\u5168\uff1a\u5916\u90e8\u62ff\u5230\u7684\u662f\u526f\u672c\uff0c\u4fee\u6539\u5b83\u4e0d\u4f1a\u5f71\u54cd\u539f\u59cb\u5bf9\u8c61\u3002<strong>\u95ee\u9898\u5728\u4e8e\u62f7\u8d1d\u6210\u672c\u3002<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u62f7\u8d1d\u4ee3\u4ef7\u4e0d\u53ef\u5ffd\u89c6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c&nbsp;<code>name_<\/code>&nbsp;\u662f\u4e00\u4e2a\u5f88\u957f\u7684\u5b57\u7b26\u4e32\uff08\u6bd4\u5982 10KB \u7684 JSON\uff09\uff0c\u6bcf\u6b21\u8c03\u7528&nbsp;<code>getName()<\/code>&nbsp;\u90fd\u5b8c\u6574\u62f7\u8d1d\u4e00\u904d\uff0c\u5728\u5faa\u73af\u6216\u9ad8\u9891\u8c03\u7528\u4e2d\u6027\u80fd\u6025\u5267\u4e0b\u964d\u3002\u8fd4\u56de&nbsp;<code>const std::string&amp;<\/code>&nbsp;\u76f4\u63a5\u6307\u5411\u5bf9\u8c61\u5185\u90e8\u7684&nbsp;<code>name_<\/code>\uff0c\u96f6\u62f7\u8d1d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const std::string&amp; getName() const {\n    return name_;  <em>\/\/ \u4e0d\u62f7\u8d1d<\/em>\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u90a3\u4e3a\u4ec0\u4e48\u4e0d\u5e72\u8106\u8fd4\u56de\u975e const \u5f15\u7528&nbsp;<code>std::string&amp;<\/code>\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u4e3a\u8fd9\u4f1a<strong>\u7834\u574f\u5c01\u88c5<\/strong>\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Person {\n    std::string name_;\npublic:\n    std::string&amp; getName() { return name_; }\n};\n\nPerson p(\"Alice\");\np.getName() = \"Bob\";   <em>\/\/ \u76f4\u63a5\u6539\u4e86\u5185\u90e8\u6210\u5458\uff01\u7c7b\u7684\u4e0d\u53d8\u91cf\u53ef\u80fd\u88ab\u7834\u574f<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c&nbsp;<code>name_<\/code>&nbsp;\u672c\u5e94\u53ea\u901a\u8fc7&nbsp;<code>setName()<\/code>&nbsp;\u4fee\u6539\uff08\u4e5f\u8bb8\u8981\u8bb0\u5f55\u4fee\u6539\u65e5\u5fd7\u3001\u89e6\u53d1\u901a\u77e5\uff09\uff0c\u76f4\u63a5\u66b4\u9732\u53ef\u53d8\u5f15\u7528\u5c31\u8ba9\u4e00\u5207\u9632\u62a4\u5931\u6548\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>const<\/code>&nbsp;\u5728\u8fd9\u91cc\u5c31\u662f<strong>\u5951\u7ea6<\/strong>\uff1a<br>\u201c\u6211\u8ba9\u4f60\u770b\uff0c\u4f46\u4e0d\u8bb8\u4f60\u6539\uff0c\u6539\u52a8\u7684\u552f\u4e00\u6e20\u9053\u662f\u6211\u63d0\u4f9b\u7684\u6210\u5458\u51fd\u6570\u3002\u201d<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e34\u65f6\u5bf9\u8c61\u751f\u547d\u5468\u671f\u7684\u7279\u6b8a\u4fdd\u62a4<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u5173\u952e\u7684\u662f\uff0c<code>const T&amp;<\/code>&nbsp;\u53ef\u4ee5<strong>\u7ed1\u5b9a\u5230\u4e34\u65f6\u5bf9\u8c61\u5e76\u5ef6\u957f\u5176\u751f\u547d\u5468\u671f<\/strong>\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const std::string&amp; str = getString(); <em>\/\/ getString() \u8fd4\u56de\u4e34\u65f6 string<\/em>\n<em>\/\/ \u6b63\u5e38\u60c5\u51b5\u4e0b\u4e34\u65f6\u5bf9\u8c61\u4f1a\u7acb\u523b\u9500\u6bc1\uff0c\u4f46 const \u5f15\u7528\u8ba9\u5b83\u6d3b\u5230 str \u79bb\u5f00\u4f5c\u7528\u57df<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u53bb\u6389&nbsp;<code>const<\/code>\uff0c\u4ec5\u7528&nbsp;<code>std::string&amp;<\/code>&nbsp;\u65e0\u6cd5\u7ed1\u5b9a\u5230\u4e34\u65f6\u5bf9\u8c61\uff0c\u4ee3\u7801\u7f16\u8bd1\u4e0d\u8fc7\u3002\u8fd9\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\uff0c\u6bd4\u5982\u5728\u8303\u56f4 for \u5faa\u73af\u4e2d\u4f7f\u7528\u4e34\u65f6\u5bb9\u5668\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for (const auto&amp; x : getVector()) { ... }  <em>\/\/ getVector() \u8fd4\u56de\u4e34\u65f6 vector<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c<code>const T&amp;<\/code>&nbsp;\u8fd4\u56de\u503c\u89e3\u51b3\u4e86\u4e24\u4e2a\u51b2\u7a81\u7684\u9700\u6c42\uff1a<strong>\u907f\u514d\u62f7\u8d1d + \u7981\u6b62\u975e\u6cd5\u4fee\u6539 + \u5b89\u5168\u7ed1\u5b9a\u4e34\u65f6\u5bf9\u8c61<\/strong>\u3002\u5b83\u7edd\u4e0d\u662f\u201c\u60f3\u590d\u6742\u4e86\u201d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001<code>const<\/code>&nbsp;\u771f\u7684\u6ca1\u7528\u5417\uff1f\u5220\u6389\u4e0d\u5c31\u5f97\u4e86\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u201c\u6211\u60f3\u6539\u7684\u8bdd\uff0c\u5728\u6e90\u7801\u4e2d\u5220\u9664 const \u5c31\u884c\u4e86\u3002\u201d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u597d\u6bd4\u8bf4\u201c\u95e8\u9501\u6ca1\u7528\uff0c\u56e0\u4e3a\u6211\u60f3\u8fdb\u53bb\u62ff\u9524\u5b50\u7838\u4e86\u5c31\u884c\u3002\u201d \u2014\u2014 \u9501\u9632\u7684\u662f<strong>\u610f\u5916\u548c\u4e0d\u9700\u8981\u6388\u6743\u7684\u95ef\u5165<\/strong>\uff0c\u4e0d\u662f\u66b4\u529b\u7834\u574f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8f6f\u4ef6\u91cc\uff0c<code>const<\/code>&nbsp;\u7684\u4f5c\u7528\u662f\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u9884\u9632\u65e0\u610f\u7684\u9519\u8bef<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u5199\u4e00\u4e2a\u51fd\u6570&nbsp;<code>void process(const BigData&amp; data)<\/code>\uff0c\u672c\u610f\u53ea\u662f\u8bfb\u53d6\u3002\u67d0\u5929\u56e2\u961f\u53e6\u4e00\u4e2a\u4eba\u4fee\u6539\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u4e0d\u5c0f\u5fc3\u5199\u4e86&nbsp;<code>data.reset()<\/code>&nbsp;\u6216\u4fee\u6539\u4e86\u5185\u90e8\u72b6\u6001\uff0c\u7f16\u8bd1\u5668\u7acb\u523b\u62a5\u9519\u3002\u5982\u679c\u6ca1\u6709&nbsp;<code>const<\/code>\uff0c\u9519\u8bef\u53ef\u80fd\u85cf\u5728\u6570\u767e\u884c\u4ee3\u7801\u6df1\u5904\uff0c\u6d4b\u8bd5\u96be\u4ee5\u8986\u76d6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u660e\u786e\u63a5\u53e3\u8bed\u4e49\uff0c\u5f62\u6210\u81ea\u6587\u6863<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">size_t size() const;   <em>\/\/ \u4e00\u770b\u5c31\u77e5\u9053\u4e0d\u4f1a\u4fee\u6539\u5bf9\u8c61<\/em>\nvoid sort();            <em>\/\/ \u53ef\u80fd\u4fee\u6539<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u7528\u8005\u65e0\u9700\u67e5\u6587\u6863\u5c31\u80fd\u660e\u767d\u51fd\u6570\u7684\u884c\u4e3a\u3002\u8fd9\u662f\u63a5\u53e3\u8bbe\u8ba1\u7684\u4e00\u90e8\u5206\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5141\u8bb8\u4f7f\u7528\u5e38\u91cf\u5bf9\u8c61<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u6709\u4e00\u4e2a&nbsp;<code>const Person<\/code>&nbsp;\u5bf9\u8c61\uff08\u53ef\u80fd\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u4f20\u8fdb\u6765\uff09\uff0c\u4f60<strong>\u53ea\u80fd\u8c03\u7528\u5b83\u7684&nbsp;<code>const<\/code>&nbsp;\u6210\u5458\u51fd\u6570<\/strong>\u3002\u8981\u662f\u4f60\u7684&nbsp;<code>getName()<\/code>&nbsp;\u6ca1\u6807&nbsp;<code>const<\/code>\uff0c\u90a3\u5bf9\u5e38\u91cf\u5bf9\u8c61\u6839\u672c\u65e0\u6cd5\u4f7f\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void printPerson(const Person&amp; p) {\n    std::cout &lt;&lt; p.getName();  <em>\/\/ \u82e5 getName() \u4e0d\u662f const\uff0c\u7f16\u8bd1\u9519\u8bef<\/em>\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u82e5\u4f60\u56e0\u4e3a\u201c\u60f3\u6539\u5c31\u53bb\u6389 const\u201d\u800c\u628a\u6240\u6709\u51fd\u6570\u90fd\u4e0d\u52a0 const\uff0c\u90a3\u4e48\u4f60\u518d\u4e5f\u65e0\u6cd5\u5904\u7406\u4efb\u4f55&nbsp;<code>const<\/code>&nbsp;\u5bf9\u8c61\uff0c\u7b49\u4e8e\u653e\u5f03\u4e86 C++ \u7c7b\u578b\u7cfb\u7edf\u4e2d\u201c\u5e38\u91cf\u6b63\u786e\u6027\u201d\u8fd9\u4e00\u5f3a\u529b\u5de5\u5177\uff0c\u8ba9\u4ee3\u7801\u53d8\u5f97\u8106\u5f31\u4e14\u96be\u4ee5\u590d\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u5e2e\u52a9\u7f16\u8bd1\u5668\u4f18\u5316<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>const<\/code>&nbsp;\u5141\u8bb8\u7f16\u8bd1\u5668\u505a\u51fa\u66f4\u6fc0\u8fdb\u7684\u4f18\u5316\uff0c\u6bd4\u5982\u5c06\u53d8\u91cf\u653e\u5165\u53ea\u8bfb\u5185\u5b58\u3001\u6d88\u9664\u91cd\u590d\u52a0\u8f7d\u7b49\u3002\u867d\u7136\u6709\u65f6\u4f18\u5316\u6548\u679c\u4e0d\u660e\u663e\uff0c\u4f46\u5728\u67d0\u4e9b\u8bed\u5883\u4e0b\u786e\u5b9e\u6709\u5f71\u54cd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u9632\u6b62\u81ea\u5df1\u6539\u81ea\u5df1\u7684\u4ee3\u7801\uff1f\u4e0d\uff0c\u662f\u5efa\u7acb\u8fb9\u754c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u5f53\u7136\u6709\u6743\u5229\u4fee\u6539\u6e90\u7801\u5220\u9664&nbsp;<code>const<\/code>\uff0c\u4f46\u8fd9\u610f\u5473\u7740\u4f60<strong>\u4e3b\u52a8\u6253\u7834\u4e86\u65e2\u5b9a\u5951\u7ea6<\/strong>\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u88ab\u5f88\u591a\u4eba\u4f7f\u7528\uff0c\u7a81\u7136\u53bb\u6389&nbsp;<code>const<\/code>&nbsp;\u53ef\u80fd\u5bfc\u81f4\u6240\u6709\u4f9d\u8d56\u53ea\u8bfb\u4fdd\u8bc1\u7684\u4ee3\u7801\u5728\u8fd0\u884c\u65f6\u5d29\u6e83\u6216\u4ea7\u751f\u8be1\u5f02 bug\u3002<code>const<\/code>&nbsp;\u662f\u4e00\u79cd\u5bf9\u7ef4\u62a4\u8005\u548c\u7f16\u8bd1\u5668\u90fd\u53ef\u89c1\u7684\u627f\u8bfa\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u603b\u7ed3\uff1a<code>const<\/code>&nbsp;\u4e0d\u662f\u67b7\u9501\uff0c\u800c\u662f\u5b89\u5168\u5e26\u3002<\/strong>&nbsp;\u4f60\u53ef\u4ee5\u4e0d\u7cfb\uff0c\u4f46\u4e8b\u6545\u53d1\u751f\u65f6\u4ee3\u4ef7\u5de8\u5927\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u4e3a\u4ec0\u4e48 C++ \u4e2d\u5f15\u7528\u591a\uff0c\u6307\u9488\u597d\u50cf\u5f88\u5c11\u51fa\u73b0\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u89c2\u5bdf\u5230\u4e86\u73b0\u4ee3 C++ \u7684\u4e00\u4e2a\u663e\u8457\u7279\u70b9\uff1a<strong>\u5f15\u7528\u4f18\u5148\uff0c\u6307\u9488\u53d7\u9650\u4f7f\u7528<\/strong>\u3002\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5f15\u7528\u66f4\u5b89\u5168\uff0c\u907f\u514d\u7a7a\u6307\u9488<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f15\u7528\u5728\u5b9a\u4e49\u65f6\u5fc5\u987b\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5408\u6cd5\u5bf9\u8c61\uff0c\u4e0d\u5b58\u5728\u201c\u7a7a\u5f15\u7528\u201d\uff08\u9664\u975e\u6545\u610f\u7834\u574f\uff09\u3002\u800c\u6307\u9488\u53ef\u4ee5\u662f&nbsp;<code>nullptr<\/code>\uff0c\u6bcf\u6b21\u4f7f\u7528\u524d\u90fd\u53ef\u80fd\u9700\u8981\u68c0\u67e5\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void f(int* p) {\n    if (p) *p = 10; <em>\/\/ \u5fc5\u987b\u68c0\u67e5<\/em>\n}\nvoid f(int&amp; r) {\n    r = 10;         <em>\/\/ \u6c38\u8fdc\u6709\u6548\uff0c\u65e0\u9700\u68c0\u67e5<\/em>\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u91cf bug \u6e90\u4e8e\u7a7a\u6307\u9488\u89e3\u5f15\u7528\uff0c\u5f15\u7528\u4ece\u8bed\u6cd5\u5c42\u9762\u6d88\u9664\u4e86\u8fd9\u79cd\u53ef\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5f15\u7528\u8bed\u6cd5\u66f4\u7b80\u6d01<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f15\u7528\u4f7f\u7528\u8d77\u6765\u50cf\u666e\u901a\u53d8\u91cf\uff0c\u4e0d\u9700\u8981&nbsp;<code>*<\/code>&nbsp;\u89e3\u5f15\u7528\uff0c\u4e5f\u4e0d\u9700\u8981&nbsp;<code>-&gt;<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void swap(int&amp; a, int&amp; b) { int t = a; a = b; b = t; }\n<em>\/\/ \u5bf9\u6bd4\u6307\u9488\u7248\u672c\uff1a<\/em>\nvoid swap(int* a, int* b) { int t = *a; *a = *b; *b = t; }<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5f15\u7528\u4e0d\u80fd\u91cd\u65b0\u7ed1\u5b9a\uff0c\u8bed\u4e49\u66f4\u6e05\u6670<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f15\u7528\u4e00\u65e6\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c31\u6c38\u8fdc\u662f\u5b83\u7684\u522b\u540d\u3002\u8fd9\u975e\u5e38\u9002\u5408\u4f20\u9012\u53c2\u6570\u3001\u8fd4\u56de\u522b\u540d\u7b49\u573a\u666f\u3002\u6307\u9488\u53ef\u4ee5\u88ab\u91cd\u65b0\u6307\u5411\u4e0d\u540c\u5bf9\u8c61\uff0c\u5bb9\u6613\u6df7\u6dc6\u610f\u56fe\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int x = 1, y = 2;\nint&amp; ref = x;\nref = y;   <em>\/\/ \u4fee\u6539x\u7684\u503c\uff0c\u800c\u4e0d\u662f\u8ba9ref\u7ed1\u5b9a\u5230y\uff08ref\u4ecd\u662fx\u7684\u522b\u540d\uff09<\/em><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u73b0\u4ee3 C++ \u4e2d\u6307\u9488\u7684\u804c\u8d23\u88ab\u66f4\u7cbe\u786e\u5730\u5212\u5206<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6240\u6709\u6743\u6307\u9488<\/strong>\u00a0\u2192 \u7528\u667a\u80fd\u6307\u9488\uff1a<code>std::unique_ptr<\/code>\u3001<code>std::shared_ptr<\/code>\u00a0\u660e\u786e\u8868\u8fbe\u8d44\u6e90\u6240\u6709\u6743\u3002<\/li>\n\n\n\n<li><strong>\u65e0\u6240\u6709\u6743\u3001\u53ef\u7a7a\u5f15\u7528<\/strong>\u00a0\u2192 \u539f\u59cb\u6307\u9488\u8fd8\u80fd\u7528\uff0c\u4f46\u901a\u5e38\u4ec5\u9650\u4e8e\u201c\u53ef\u80fd\u4e0d\u6307\u5411\u4efb\u4f55\u5bf9\u8c61\u201d\u4e14\u65e0\u9700\u6240\u6709\u6743\u7684\u573a\u666f\u3002<\/li>\n\n\n\n<li><strong>\u65e0\u6240\u6709\u6743\u3001\u5fc5\u987b\u5b58\u5728\u7684\u5f15\u7528<\/strong>\u00a0\u2192 \u7528\u5f15\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8e\u662f\uff0c\u539f\u59cb\u6307\u9488\u7684\u51fa\u573a\u673a\u4f1a\u5927\u5927\u51cf\u5c11\uff0c\u4ec5\u5728\u9700\u8981\u663e\u5f0f\u8868\u793a\u201c\u53ef\u80fd\u4e3a\u7a7a\u201d\u6216\u8005\u4e0e C \u63a5\u53e3\u4ea4\u4e92\u65f6\u624d\u7528\u3002\u5f15\u7528\u6210\u4e86\u9ed8\u8ba4\u7684\u95f4\u63a5\u8bbf\u95ee\u624b\u6bb5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u8303\u56f4 for \u5faa\u73af\u3001\u79fb\u52a8\u8bed\u4e49\u7b49\u90fd\u5f3a\u4f9d\u8d56\u5f15\u7528<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for (auto&amp; elem : vec) { elem *= 2; } <em>\/\/ \u5f15\u7528\u4fee\u6539\u5143\u7d20<\/em>\nfor (const auto&amp; elem : vec) { ... }  <em>\/\/ \u907f\u514d\u62f7\u8d1d<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C++11 \u4ee5\u540e\uff0c\u5199\u201c\u5f15\u7528\u201d\u6bd4\u201c\u6307\u9488\u201d\u66f4\u7b26\u5408\u8868\u8fbe\u201c\u904d\u5386\u5e76\u64cd\u4f5c\u201d\u7684\u76f4\u89c9\uff0c\u6240\u4ee5\u66f4\u52a0\u6cdb\u6ee5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6700\u540e\uff0c\u56de\u5230\u4f60\u7684\u89c2\u70b9<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u201c\u6240\u6709\u7684\u4e1c\u897f\u90fd\u4e0d\u52a0 const\uff0c\u81ea\u5df1\u6ce8\u610f\u70b9\u5c31\u884c\u4e86\u201d \u2014\u2014 \u5bf9\u4e8e\u51e0\u5341\u884c\u7684\u5c0f\u7a0b\u5e8f\u53ef\u4ee5\uff0c\u4f46\u8f6f\u4ef6\u89c4\u6a21\u4e00\u65e6\u53d8\u5927\uff0c\u4eba\u7684\u6ce8\u610f\u529b\u3001\u56e2\u961f\u534f\u4f5c\u3001\u957f\u671f\u7ef4\u62a4\u90fd\u9700\u8981\u7f16\u8bd1\u5668\u5e2e\u4f60\u628a\u201c\u6ce8\u610f\u70b9\u201d\u53d8\u6210\u201c\u5f3a\u5236\u7ea6\u675f\u201d\u3002<code>const<\/code>&nbsp;\u5c31\u662f\u8fd9\u6837\u4e00\u4e2a\u514d\u8d39\u4e14\u5f3a\u5927\u7684\u9759\u6001\u68c0\u67e5\u5de5\u5177\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u201cconst \u5f88\u6ca1\u7528\uff0c\u6211\u60f3\u6539\u5c31\u5220\u6389\u201d \u2014\u2014 \u8fd9\u76f8\u5f53\u4e8e\u8bf4\u201c\u5408\u7ea6\u6ca1\u7528\uff0c\u6495\u6bc1\u5c31\u884c\u201d\u3002\u4f46\u6495\u6bc1\u5408\u7ea6\u7684\u4ee3\u4ef7\u662f\u6574\u4e2a\u7cfb\u7edf\u5bf9\u5408\u7ea6\u7684\u4fe1\u4efb\u5d29\u584c\uff0c\u5f15\u53d1\u7684\u8fde\u9501\u9519\u8bef\u96be\u4ee5\u9884\u6599\u3002<code>const<\/code>&nbsp;\u662f\u7ed9\u4f60\u548c\u4ee3\u7801\u7684\u5176\u4ed6\u4f7f\u7528\u8005\u4e4b\u95f4\u5efa\u7acb\u4fe1\u4efb\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e0c\u671b\u8fd9\u4e9b\u89e3\u91ca\u80fd\u8ba9\u4f60\u770b\u5230\uff0c<code>const<\/code>&nbsp;\u548c\u5f15\u7528\u7684\u201c\u590d\u6742\u201d\uff0c\u5b9e\u9645\u4e0a\u662f\u7528\u5f88\u5c0f\u7684\u4ee3\u4ef7\uff0c\u6362\u53d6\u4e86<strong>\u5b89\u5168\u3001\u9ad8\u6548\u3001\u6e05\u6670<\/strong>\u7684\u4ee3\u7801\u3002\u8fd9\u4e0d\u662f\u6545\u4f5c\u9ad8\u6df1\uff0c\u800c\u662f C++ \u5728\u6570\u5341\u5e74\u7684\u5de5\u7a0b\u5b9e\u8df5\u4e2d\u6c89\u6dc0\u51fa\u7684\u7cbe\u9ad3\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-af1fd8236218eacc363ec67981398255 wp-block-paragraph\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5f15\u7528\u7684\u6838\u5fc3\u4ef7\u503c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f15\u7528\uff08reference\uff09\u7684\u672c\u8d28\u662f<strong>\u522b\u540d<\/strong>\uff0c\u5b83\u4e0d\u62e5\u6709\u5bf9\u8c61\uff0c\u4f7f\u7528\u4e0a\u4e0e\u539f\u540d\u65e0\u5f02\u3002\u5f15\u5165\u5b83\u7684\u6838\u5fc3\u539f\u56e0\u6709\u4e09\u4e2a\uff1a<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u62f7\u8d1d<\/strong>\u00a0\u2014\u2014 \u4f20\u9012\u5927\u5bf9\u8c61\u65f6\u96f6\u5f00\u9500\uff0c\u76f4\u63a5\u64cd\u4f5c\u539f\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>\u5141\u8bb8\u4fee\u6539\u5b9e\u53c2<\/strong>\u00a0\u2014\u2014 \u975e const \u5f15\u7528\u8ba9\u51fd\u6570\u80fd\u591f\u4fee\u6539\u5916\u90e8\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u6027\u4e0e\u4fbf\u5229\u6027<\/strong>\u00a0\u2014\u2014 \u5f15\u7528\u5fc5\u987b\u7ed1\u5b9a\u5408\u6cd5\u5bf9\u8c61\uff08\u65e0\u7a7a\u5f15\u7528\uff09\uff0c\u8bed\u6cd5\u66f4\u7b80\u6d01\uff0c\u65e0\u9700\u00a0<code>*<\/code>\u00a0\u548c\u00a0<code>-><\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5177\u4f53\u5e94\u7528\u573a\u666f\u4e0e\u4ee3\u7801\u793a\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u975e const \u5de6\u503c\u5f15\u7528&nbsp;<code>T&amp;<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u573a\u666f\uff1a\u9700\u8981\u4fee\u6539\u4f20\u5165\u7684\u5b9e\u53c2\uff0c\u6216\u8005\u8fd4\u56de\u4e00\u4e2a\u53ef\u4fee\u6539\u7684\u522b\u540d\u3002<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\uff081\uff09\u4fee\u6539\u5b9e\u53c2\u7684\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void swap(int&amp; a, int&amp; b) {\n    int temp = a;\n    a = b;\n    b = temp;\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\uff082\uff09\u8fd4\u56de\u7c7b\u6210\u5458\uff0c\u5141\u8bb8\u5916\u90e8\u4fee\u6539\uff08\u5982&nbsp;<code>operator[]<\/code>\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class IntArray {\n    int data[100];\npublic:\n    int&amp; operator[](size_t index) {\n        return data[index];   <em>\/\/ \u5916\u90e8\u53ef\u8d4b\u503c\uff1aarr[0] = 5;<\/em>\n    }\n};<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\uff083\uff09\u94fe\u5f0f\u8c03\u7528\uff08\u8fd4\u56de&nbsp;<code>*this<\/code>\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Builder {\npublic:\n    Builder&amp; setA(int a) { <em>\/*...*\/<\/em> return *this; }\n    Builder&amp; setB(int b) { <em>\/*...*\/<\/em> return *this; }\n};\n\nBuilder b;\nb.setA(1).setB(2);  <em>\/\/ \u6d41\u5f0f\u63a5\u53e3<\/em><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\uff084\uff09\u8303\u56f4 for \u5faa\u73af\u4e2d\u4fee\u6539\u5143\u7d20<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">std::vector&lt;int&gt; vec = {1, 2, 3};\nfor (auto&amp; x : vec) {\n    x *= 2;   <em>\/\/ \u4fee\u6539\u5bb9\u5668\u5185\u6bcf\u4e2a\u5143\u7d20<\/em>\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. const \u5de6\u503c\u5f15\u7528&nbsp;<code>const T&amp;<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u573a\u666f\uff1a\u53ea\u8bfb\u8bbf\u95ee\u5bf9\u8c61\uff0c\u5e76\u4e14\u5e0c\u671b\u907f\u514d\u62f7\u8d1d\u5f00\u9500\u3002\u5b83\u662f C++ \u4e2d\u6700\u5e38\u89c1\u7684\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\u4e4b\u4e00\u3002<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\uff081\uff09\u53ea\u8bfb\u53c2\u6570\uff0c\u907f\u514d\u62f7\u8d1d\u5927\u5bf9\u8c61<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/\/ \u5047\u8bbe LargeData \u62f7\u8d1d\u6210\u672c\u6781\u9ad8<\/em>\nvoid process(const LargeData&amp; data) {\n    <em>\/\/ \u53ea\u8bfb\u53d6 data\uff0c\u96f6\u62f7\u8d1d<\/em>\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\uff082\uff09\u53ea\u8bfb\u8fd4\u56de\u503c\uff0c\u66b4\u9732\u5185\u90e8\u6210\u5458\u4f46\u4e0d\u5141\u8bb8\u4fee\u6539<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Person {\n    std::string name_;\npublic:\n    const std::string&amp; getName() const {\n        return name_;   <em>\/\/ \u5916\u90e8\u53ea\u80fd\u8bfb\uff0c\u4e0d\u80fd\u901a\u8fc7\u8be5\u5f15\u7528\u4fee\u6539<\/em>\n    }\n};<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\uff083\uff09\u7ed1\u5b9a\u53f3\u503c\uff0c\u5ef6\u957f\u4e34\u65f6\u5bf9\u8c61\u751f\u547d\u5468\u671f<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const std::string&amp; ref = getString(); <em>\/\/ getString \u8fd4\u56de\u4e34\u65f6 string<\/em>\n<em>\/\/ \u4e34\u65f6\u5bf9\u8c61\u539f\u672c\u4f1a\u7acb\u523b\u6790\u6784\uff0c\u4f46 const \u5f15\u7528\u8ba9\u5b83\u6d3b\u5230 ref \u79bb\u5f00\u4f5c\u7528\u57df<\/em>\nstd::cout &lt;&lt; ref &lt;&lt; std::endl;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u7279\u6027\u5141\u8bb8\u5b89\u5168\u5730\u4f7f\u7528\u4e34\u65f6\u5bf9\u8c61\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u8303\u56f4 for \u5faa\u73af\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for (const auto&amp; item : createVector()) {  <em>\/\/ createVector \u8fd4\u56de\u4e34\u65f6 vector<\/em>\n    <em>\/\/ ...<\/em>\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\uff084\uff09\u8303\u56f4 for \u5faa\u73af\u4e2d\u53ea\u8bfb\u904d\u5386\uff0c\u907f\u514d\u62f7\u8d1d<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for (const auto&amp; elem : largeContainer) {\n    <em>\/\/ \u53ea\u8bfb elem\uff0c\u4e0d\u62f7\u8d1d\u6bcf\u4e2a\u5143\u7d20<\/em>\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\uff085\uff09\u591a\u6001\uff1a\u901a\u8fc7\u57fa\u7c7b\u5f15\u7528\u64cd\u4f5c\u6d3e\u751f\u7c7b\u5bf9\u8c61<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Animal { public: virtual void speak() const = 0; };\nclass Dog : public Animal { public: void speak() const override { ... } };\n\nvoid talk(const Animal&amp; a) {\n    a.speak();   <em>\/\/ \u591a\u6001\u8c03\u7528\uff0c\u65e0\u9700\u62f7\u8d1d<\/em>\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u53f3\u503c\u5f15\u7528&nbsp;<code>T&amp;&amp;<\/code>\uff08\u975e const \u5f15\u7528\u7684\u4e00\u79cd\u7279\u6b8a\u5f62\u5f0f\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u573a\u666f\uff1a\u79fb\u52a8\u8bed\u4e49\u3001\u5b8c\u7f8e\u8f6c\u53d1\u3002\u867d\u7136\u4e0d\u662f\u201cconst\/\u975econst\u201d\u7684\u4e3b\u8981\u8ba8\u8bba\u70b9\uff0c\u4f46\u4e5f\u662f\u5f15\u7528\u91cd\u8981\u5e94\u7528\u3002<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\uff081\uff09\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4e0e\u79fb\u52a8\u8d4b\u503c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Buffer {\n    char* ptr;\npublic:\n    Buffer(Buffer&amp;&amp; other) noexcept : ptr(other.ptr) {\n        other.ptr = nullptr;   <em>\/\/ \u8f6c\u79fb\u8d44\u6e90<\/em>\n    }\n    Buffer&amp; operator=(Buffer&amp;&amp; other) noexcept { ... return *this; }\n};<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\uff082\uff09\u5b8c\u7f8e\u8f6c\u53d1\uff08\u4e07\u80fd\u5f15\u7528&nbsp;<code>T&amp;&amp;<\/code>\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">template&lt;typename T, typename Arg&gt;\nT create(Arg&amp;&amp; arg) {\n    return T(std::forward&lt;Arg&gt;(arg));  <em>\/\/ \u4fdd\u6301\u5de6\u503c\/\u53f3\u503c\u5c5e\u6027<\/em>\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001const \u5f15\u7528\u662f\u4e0d\u662f\u5927\u591a\u6570\u60c5\u51b5\u90fd\u4f18\u4e8e\u503c\u4f20\u9012\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u8bba\uff1a\u5bf9\u4e8e\u53ea\u8bfb\u7684\u975e\u57fa\u672c\u7c7b\u578b\uff08\u7279\u522b\u662f\u5927\u5bf9\u8c61\uff09\uff0cconst \u5f15\u7528\u660e\u663e\u4f18\u4e8e\u503c\u4f20\u9012\uff1b\u4f46\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u548c\u5c0f\u578b POD\uff0c\u503c\u4f20\u9012\u5f80\u5f80\u66f4\u4f18\u6216\u6027\u80fd\u76f8\u540c\u4f46\u8bed\u4e49\u66f4\u7b80\u5355\u3002\u5e76\u4e0d\u5b58\u5728\u201c\u5927\u591a\u6570\u60c5\u51b5\u901a\u7528\u201d\u7684\u7edd\u5bf9\u4f18\u52bf\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5177\u4f53\u5206\u6790\u5982\u4e0b\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5f53\u7c7b\u578b\u5f88\u5927\uff08\u5982&nbsp;<code>std::string<\/code>,&nbsp;<code>std::vector<\/code>, \u81ea\u5b9a\u4e49\u5927\u7ed3\u6784\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>const \u5f15\u7528<\/strong>\uff1a\u96f6\u62f7\u8d1d\uff0c\u53ea\u4f20\u9012\u4e00\u4e2a\u6307\u9488\uff08\u901a\u5e38 8 \u5b57\u8282\uff09\uff0c\u5e76\u4e14\u907f\u514d\u6df1\u62f7\u8d1d\u7684\u5de8\u5927\u5f00\u9500\u3002<br>\u2705 \u4f18\u9009\uff0c\u662f\u6807\u51c6\u505a\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void analyze(const BigMatrix&amp; m);  <em>\/\/ \u6beb\u65e0\u7591\u95ee\u7528 const \u5f15\u7528<\/em><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5f53\u7c7b\u578b\u662f\u57fa\u672c\u7c7b\u578b\uff08<code>int<\/code>,&nbsp;<code>double<\/code>,&nbsp;<code>char<\/code>&nbsp;\u7b49\uff09\u6216\u5c0f\u578b\u7b80\u5355\u7ed3\u6784\uff08<code>Point<\/code>&nbsp;\u4e24\u4e2a&nbsp;<code>int<\/code>\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u503c\u4f20\u9012<\/strong>\uff1a\u76f4\u63a5\u653e\u5728\u5bc4\u5b58\u5668\u6216\u6808\u4e0a\uff0c\u5f00\u9500\u6781\u5c0f\uff1b\u5f15\u7528\u5e95\u5c42\u5b9e\u73b0\u4e3a\u6307\u9488\uff0c\u9700\u8981\u95f4\u63a5\u5bfb\u5740\uff0c\u53ef\u80fd\u66f4\u6162\u3002<\/li>\n\n\n\n<li><strong>const \u5f15\u7528<\/strong>\uff1a\u6bcf\u6b21\u8bbf\u95ee\u90fd\u8981\u89e3\u5f15\u7528\uff0c\u4e14\u7f16\u8bd1\u5668\u66f4\u96be\u4f18\u5316\uff08\u56e0\u4e3a\u6709\u6307\u9488\u522b\u540d\u95ee\u9898\uff09\u3002<br>\u2705 \u503c\u4f20\u9012\u901a\u5e38\u66f4\u597d\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void increment(int x);           <em>\/\/ \u597d<\/em>\nvoid increment(const int&amp; x);    <em>\/\/ \u591a\u6b64\u4e00\u4e3e\uff0c\u8fd8\u964d\u4f4e\u6027\u80fd<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e&nbsp;<code>std::pair&lt;int, int&gt;<\/code>&nbsp;\u6216&nbsp;<code>struct Point { int x,y; };<\/code>\uff0c\u73b0\u4ee3 ABI \u5e38\u901a\u8fc7\u5bc4\u5b58\u5668\u4f20\u503c\uff0c\u503c\u4f20\u9012\u96f6\u5f00\u9500\uff1bconst \u5f15\u7528\u53cd\u800c\u589e\u52a0\u5185\u5b58\u8bbf\u95ee\uff0c\u53ef\u80fd\u6210\u4e3a\u4f18\u5316\u969c\u788d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u51fd\u6570\u5185\u90e8\u65e0\u8bba\u5982\u4f55\u90fd\u9700\u8981\u4e00\u4efd\u526f\u672c\uff08\u5982 setter\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u503c\u4f20\u9012 + \u79fb\u52a8<\/strong>\uff1a\u517c\u987e\u5de6\u503c\u548c\u53f3\u503c\uff0c\u7f16\u8bd1\u5668\u53ef\u76f4\u63a5\u5728\u53c2\u6570\u6784\u9020\u65f6\u8fdb\u884c\u4f18\u5316\uff08\u590d\u5236\u7701\u7565\uff09\u3002<\/li>\n\n\n\n<li><strong>const \u5f15\u7528 + \u62f7\u8d1d<\/strong>\uff1a\u5bf9\u4e8e\u5de6\u503c\u4f1a\u5f3a\u5236\u62f7\u8d1d\u4e00\u6b21\uff1b\u5bf9\u4e8e\u53f3\u503c\u9700\u8981\u62f7\u8d1d\uff0c\u65e0\u6cd5\u79fb\u52a8\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Holder {\n    std::string name;\npublic:\n    <em>\/\/ \u8fd9\u79cd\u5199\u6cd5\u6bd4 const std::string&amp; + \u62f7\u8d1d\u66f4\u7075\u6d3b<\/em>\n    void setName(std::string n) { name = std::move(n); }\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u65f6\u53cd\u800c<strong>\u503c\u4f20\u9012<\/strong>\u66f4\u4f18\uff0cconst \u5f15\u7528\u662f\u6b21\u9009\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u9700\u8981\u5b8c\u7f8e\u8f6c\u53d1\u7684\u6a21\u677f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">template&lt;typename T&gt;\nvoid relay(T&amp;&amp; t) { ... }  <em>\/\/ \u4e07\u80fd\u5f15\u7528\uff0c\u4e0d\u662f const \u5f15\u7528<\/em><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6700\u7ec8\u5efa\u8bae\uff1a\u6309\u4ee5\u4e0b\u89c4\u5219\u9009\u62e9<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u53c2\u6570\u7c7b\u578b \/ \u9700\u6c42<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u4f20\u9012\u65b9\u5f0f<\/th><\/tr><\/thead><tbody><tr><td>\u57fa\u672c\u7c7b\u578b\u3001\u5c0f\u7ed3\u6784\u4f53\uff0816 \u5b57\u8282\u4ee5\u5185\uff09\u4e14\u53ea\u8bfb<\/td><td>\u503c\u4f20\u9012&nbsp;<code>T<\/code><\/td><\/tr><tr><td>\u5927\u5bf9\u8c61\u3001\u975e\u57fa\u672c\u7c7b\u578b\uff0c\u53ea\u8bfb<\/td><td><code>const T&amp;<\/code><\/td><\/tr><tr><td>\u9700\u8981\u4fee\u6539\u5b9e\u53c2<\/td><td><code>T&amp;<\/code><\/td><\/tr><tr><td>\u51fd\u6570\u5185\u90e8\u9700\u8981\u4fdd\u6709\u526f\u672c\uff08setter\uff09<\/td><td>\u503c\u4f20\u9012&nbsp;<code>T<\/code>&nbsp;+&nbsp;<code>std::move<\/code><\/td><\/tr><tr><td>\u8f6c\u53d1\u53c2\u6570\uff08\u6a21\u677f\uff09<\/td><td><code>T&amp;&amp;<\/code>&nbsp;+&nbsp;<code>std::forward<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e00\u53e5\u8bdd\u603b\u7ed3<\/strong>\uff1aconst \u5f15\u7528\u4e0d\u662f\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u4f18\u4e8e\u503c\u4f20\u9012\uff0c\u800c\u662f\u5728\u201c\u53ea\u8bfb\u5927\u5bf9\u8c61\u201d\u8fd9\u4e2a\u573a\u666f\u4e0b\u65e0\u53ef\u66ff\u4ee3\u3002\u76f2\u76ee\u5bf9\u6240\u6709\u53c2\u6570\u52a0&nbsp;<code>const&amp;<\/code>\uff08\u5305\u62ec&nbsp;<code>int<\/code>\uff09\u662f C++ \u65b0\u624b\u5e38\u89c1\u8bef\u533a\u3002\u7406\u89e3\u4e86\u8fd9\u4e9b\u573a\u666f\u4e0e\u6210\u672c\uff0c\u4f60\u5c31\u80fd\u505a\u51fa\u6700\u5408\u9002\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-420ca485239926b319683d8c51ab37c3 wp-block-paragraph\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5f15\u7528\u7684\u5e95\u5c42\u771f\u76f8\uff1a\u5e26\u201c\u8bed\u6cd5\u7cd6\u201d\u7684\u6307\u9488<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7f16\u8bd1\u5668\u5b9e\u73b0\u5c42\u9762\uff0c<strong>\u5f15\u7528\u51e0\u4e4e\u603b\u662f\u7528\u6307\u9488\u6765\u4ee3\u8868\u7684<\/strong>\uff0c\u4f46\u5b83\u88ab\u5305\u88f9\u4e86\u4e00\u5c42\u5b89\u5168\u7684\u8bed\u6cd5\u7cd6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f15\u7528\u5728\u5b9a\u4e49\u65f6\u5fc5\u987b\u521d\u59cb\u5316\uff08\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5408\u6cd5\u5bf9\u8c61\uff09\uff0c\u4e0d\u5b58\u5728\u201c\u7a7a\u5f15\u7528\u201d\u3002<\/li>\n\n\n\n<li>\u5f15\u7528\u4e00\u65e6\u7ed1\u5b9a\uff0c\u5c31\u4e0d\u80fd\u518d\u6539\u4e3a\u6307\u5411\u5176\u4ed6\u5bf9\u8c61\uff08\u76f8\u5f53\u4e8e\u00a0<code>T* const<\/code>\uff0c\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf\uff09\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u5f15\u7528\u65f6\uff0c\u4e0d\u9700\u8981\u00a0<code>*<\/code>\u00a0\u89e3\u5f15\u7528\uff0c\u76f4\u63a5\u50cf\u539f\u5bf9\u8c61\u4e00\u6837\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u5bf9\u4e8e\u51fd\u6570\u53c2\u6570&nbsp;<code>void func(ss&amp; param)<\/code>\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u628a\u5b83\u5f53\u6210\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void func(ss* const param_ptr)   <em>\/\/ \u6307\u9488\u672c\u8eab\u4e0d\u53ef\u53d8\uff0c\u4f46\u6307\u5411\u7684\u5bf9\u8c61\u53ef\u53d8\uff08\u9664\u975e\u662fconst\u5f15\u7528\uff09<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u8c03\u7528\u5904\u7684&nbsp;<code>func(my_ss)<\/code>\uff0c\u5219\u88ab\u7f16\u8bd1\u5668\u8f6c\u6362\u4e3a\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">func(&amp;my_ss);   <em>\/\/ \u4f20\u9012\u5bf9\u8c61\u5730\u5740<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u5185\u90e8\u5bf9&nbsp;<code>param<\/code>&nbsp;\u7684\u6210\u5458\u8bbf\u95ee&nbsp;<code>param.x<\/code>\uff0c\u5219\u53d8\u6210&nbsp;<code>param_ptr-&gt;x<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u4ee5\u4f60\u7684\u4f8b\u5b50\u8be6\u7ec6\u62c6\u89e3\uff1a<code>func(ss_obj);<\/code>&nbsp;\u53d1\u751f\u4e86\u4ec0\u4e48\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">struct ss {\n    int a;\n    double b;\n    std::string c;\n};\n\nvoid func(ss&amp; param) {\n    param.a = 10;          <em>\/\/ \u4fee\u6539<\/em>\n    int x = param.b;       <em>\/\/ \u8bfb\u53d6<\/em>\n}\n\nint main() {\n    ss obj{1, 2.0, \"hello\"};\n    func(obj);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u5206\u6790\u8c03\u7528&nbsp;<code>func(obj)<\/code>&nbsp;\u65f6\u7684\u5185\u5b58\u548c\u62f7\u8d1d\u60c5\u51b5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u8c03\u7528\u524d\uff1a\u5bf9\u8c61\u5728\u54ea\u91cc\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>obj<\/code>&nbsp;\u662f&nbsp;<code>main<\/code>&nbsp;\u51fd\u6570\u6808\u4e0a\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5360\u636e\u4e00\u6bb5\u8fde\u7eed\u5185\u5b58\uff0c\u6bd4\u5982\u5730\u5740&nbsp;<code>0x1000<\/code>\uff0c\u5305\u542b&nbsp;<code>int a<\/code>,&nbsp;<code>double b<\/code>, \u4e00\u4e2a&nbsp;<code>std::string<\/code>&nbsp;\u5bf9\u8c61\uff08\u53ef\u80fd\u5305\u542b\u6307\u5411\u5806\u7684\u6307\u9488\u7b49\uff09\u3002\u603b\u5927\u5c0f\u5047\u8bbe\u4e3a 40 \u5b57\u8282\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f20\u9012\u53c2\u6570\uff1a\u6ca1\u6709\u62f7\u8d1d\u5bf9\u8c61\u672c\u8eab<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u7528&nbsp;<code>func(obj)<\/code>&nbsp;\u65f6\uff0c<strong>\u4e0d\u4f1a\u8c03\u7528&nbsp;<code>ss<\/code>&nbsp;\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4e0d\u4f1a\u590d\u5236&nbsp;<code>obj<\/code>&nbsp;\u7684\u4efb\u4f55\u6570\u636e\u6210\u5458<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u4e3a\u51fd\u6570\u671f\u671b\u7684\u662f&nbsp;<code>ss&amp;<\/code>\uff0c\u7f16\u8bd1\u5668\u5904\u7406\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8ba1\u7b97\u51fa\u00a0<code>obj<\/code>\u00a0\u7684\u8d77\u59cb\u5730\u5740\uff08\u5047\u8bbe\u4e3a\u00a0<code>0x1000<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u5c06\u8be5\u5730\u5740\u4f20\u9012\u8fdb\u00a0<code>func<\/code>\u3002\u5728\u5178\u578b\u7684\u8c03\u7528\u7ea6\u5b9a\uff08\u5982 x86-64 System V\uff09\u4e0b\uff0c\u8fd9\u4e2a\u5730\u5740\u53ef\u80fd\u76f4\u63a5\u901a\u8fc7\u5bc4\u5b58\u5668\uff08\u5982\u00a0<code>RDI<\/code>\uff09\u4f20\u9012\uff0c\u800c\u4e0d\u9700\u8981\u538b\u6808\u3002<\/li>\n\n\n\n<li><code>func<\/code>\u00a0\u5185\u90e8\u7684\u5f62\u53c2\u00a0<code>param<\/code>\u00a0\u5728\u6982\u5ff5\u4e0a\u5360\u7528\u4e00\u4e2a\u6307\u9488\u5927\u5c0f\u7684\u5b58\u50a8\u7a7a\u95f4\uff08\u5982\u679c\u5bc4\u5b58\u5668\u4e0d\u8db3\u4ee5\u4fdd\u5b58\u6240\u6709\u53c2\u6570\uff0c\u53ef\u80fd\u4f1a\u6ea2\u5230\u6808\u4e0a\uff09\uff0c\u5b58\u50a8\u7684\u5c31\u662f\u00a0<code>0x1000<\/code>\u00a0\u8fd9\u4e2a\u5730\u5740\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u4f20\u9012\u5f15\u7528<strong>\u53ea\u590d\u5236\u4e86\u4e00\u4e2a\u5730\u5740\uff088\u5b57\u8282\u6307\u9488\uff09<\/strong>\uff0c\u65e0\u8bba&nbsp;<code>ss<\/code>&nbsp;\u7ed3\u6784\u4f53\u6709\u591a\u5927\uff0c\u5f00\u9500\u6052\u5b9a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u51fd\u6570\u5185\u90e8\uff1a\u64cd\u4f5c\u5f15\u7528\u5c31\u662f\u95f4\u63a5\u8bbf\u95ee\u539f\u5bf9\u8c61<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728&nbsp;<code>func<\/code>&nbsp;\u5185\u90e8\uff0c\u4f60\u5199&nbsp;<code>param.a = 10;<\/code>\uff0c\u7f16\u8bd1\u5668\u751f\u6210\u7684\u4ee3\u7801\u5927\u81f4\u662f\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">text<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mov rax, param_ptr      ; \u62ff\u5230\u5730\u5740 0x1000\nmov dword ptr [rax], 10  ; \u5c06 10 \u5199\u5165 0x1000 \u5f00\u59cb\u76844\u5b57\u8282\uff08int a\uff09<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>param.b<\/code>&nbsp;\u7684\u8bfb\u53d6\u7c7b\u4f3c\uff0c\u4ece&nbsp;<code>0x1000 + offset_of(b)<\/code>&nbsp;\u5904\u52a0\u8f7d 8 \u5b57\u8282\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u8ddf\u4f20\u9012\u6307\u9488\u540e\u4f7f\u7528&nbsp;<code>param_ptr-&gt;a<\/code>&nbsp;\u751f\u6210\u7684\u673a\u5668\u7801\u5b8c\u5168\u4e00\u6837\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u5bf9\u6bd4\u503c\u4f20\u9012\uff1a\u5de8\u5927\u7684\u5dee\u5f02<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u51fd\u6570\u7b7e\u540d\u662f&nbsp;<code>void func(ss param)<\/code>\uff08\u6309\u503c\u4f20\u9012\uff09\uff0c\u8c03\u7528&nbsp;<code>func(obj)<\/code>&nbsp;\u65f6\u4f1a\uff1a<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>\u8c03\u7528\u00a0<code>ss<\/code>\u00a0\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/strong>\uff0c\u5728\u00a0<code>func<\/code>\u00a0\u7684\u6808\u5e27\uff08\u6216\u4e34\u65f6\u533a\u57df\uff09\u6784\u9020\u4e00\u4e2a\u5168\u65b0\u7684\u00a0<code>ss<\/code>\u00a0\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u8fd9\u4e2a\u62f7\u8d1d\u8fc7\u7a0b\u4f1a\u590d\u5236\u00a0<code>a<\/code>\u3001<code>b<\/code>\uff0c\u5e76\u4e14\u56e0\u4e3a\u00a0<code>std::string c<\/code>\u00a0\u7684\u5b58\u5728\uff0c\u4f1a<strong>\u5206\u914d\u65b0\u7684\u5806\u5185\u5b58<\/strong>\u5e76\u62f7\u8d1d\u5b57\u7b26\u4e32\u5185\u5bb9\uff08\u6df1\u62f7\u8d1d\uff09\u3002<\/li>\n\n\n\n<li>\u62f7\u8d1d\u6210\u672c\u5f88\u9ad8\uff1aCPU \u65f6\u95f4\u3001\u5185\u5b58\u5206\u914d\u3001\u4ee5\u53ca\u540e\u7eed\u6790\u6784\u6210\u672c\u3002\u4fee\u6539\u00a0<code>param<\/code>\u00a0\u4e0d\u4f1a\u5f71\u54cd\u00a0<code>main<\/code>\u00a0\u4e2d\u7684\u00a0<code>obj<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5185\u5b58\u793a\u610f\u56fe\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">text<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Main \u6808:                 Func \u6808:\nobj @ 0x1000            param @ 0x2000 (\u62f7\u8d1d)\n[ a, b, c.ptr -&gt; heap1 ]   [ a, b, c.ptr -&gt; heap2 (\u5185\u5bb9\u76f8\u540c\uff0c\u4f46\u72ec\u7acb) ]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u5219\u6ca1\u6709&nbsp;<code>0x2000<\/code>&nbsp;\u7684\u62f7\u8d1d\u548c&nbsp;<code>heap2<\/code>\uff0c<code>func<\/code>&nbsp;\u76f4\u63a5\u64cd\u4f5c&nbsp;<code>0x1000<\/code>&nbsp;\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u7528\u6307\u9488\u65b9\u5f0f\u624b\u52a8\u6a21\u62df\u5f15\u7528\u4f20\u9012<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53ef\u4ee5\u7528\u6307\u9488\u6765\u5199\u4e00\u4e2a\u7b49\u6548\u5b9e\u73b0\uff0c\u4ee5\u7aa5\u63a2\u7f16\u8bd1\u5668\u7684\u5de5\u4f5c\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void func_ptr(ss* const param_ptr) {   <em>\/\/ \u5bf9\u5e94 void func(ss&amp; param)<\/em>\n    param_ptr-&gt;a = 10;\n    int x = param_ptr-&gt;b;\n}\n\nint main() {\n    ss obj{1, 2.0, \"hello\"};\n    func_ptr(&amp;obj);   <em>\/\/ \u5bf9\u5e94 func(obj)<\/em>\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e24\u8005\u7684\u5e95\u5c42\u673a\u5668\u7801\u51e0\u4e4e\u76f8\u540c\uff0c\u5dee\u5f02\u4ec5\u5728\u4e8e\u8bed\u6cd5\u3002\u5f15\u7528\u8ba9\u4f60\u53ef\u4ee5\u5199&nbsp;<code>param.a<\/code>\uff0c\u800c\u6307\u9488\u5fc5\u987b\u5199&nbsp;<code>param_ptr-&gt;a<\/code>&nbsp;\u6216&nbsp;<code>(*param_ptr).a<\/code>\u3002\u540c\u65f6\uff0c\u5f15\u7528\u5f3a\u5236\u975e\u7a7a\uff0c\u8c03\u7528\u65f6\u4e0d\u5fc5\u663e\u5f0f\u53d6\u5730\u5740\uff0c\u5199\u6cd5\u66f4\u81ea\u7136\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001<code>const<\/code>&nbsp;\u5f15\u7528\u4e0e\u975e&nbsp;<code>const<\/code>&nbsp;\u5f15\u7528\u7684\u5e95\u5c42\u533a\u522b<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u975e\u00a0<code>const<\/code>\u00a0\u5f15\u7528\u00a0<code>T&amp;<\/code><\/strong>\uff1a\u5e95\u5c42\u7684\u6307\u9488\u662f\u00a0<code>T* const<\/code>\uff08\u6307\u9488\u81ea\u8eab\u4e0d\u53ef\u53d8\uff0c\u6307\u5411\u7684\u5bf9\u8c61\u53ef\u4fee\u6539\uff09\u3002<\/li>\n\n\n\n<li><strong><code>const<\/code>\u00a0\u5f15\u7528\u00a0<code>const T&amp;<\/code><\/strong>\uff1a\u5e95\u5c42\u7684\u6307\u9488\u662f\u00a0<code>const T* const<\/code>\uff08\u6307\u9488\u81ea\u8eab\u4e0d\u53ef\u53d8\uff0c\u6307\u5411\u7684\u5bf9\u8c61\u4e5f\u4e0d\u53ef\u4fee\u6539\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u751f\u6210\u7684\u4ee3\u7801\u770b\uff0c\u6ca1\u6709\u6027\u80fd\u5dee\u5f02\uff0c\u533a\u522b\u5728\u4e8e<strong>\u7f16\u8bd1\u671f\u7c7b\u578b\u68c0\u67e5<\/strong>\uff1a\u5982\u679c\u4f60\u8bd5\u56fe\u901a\u8fc7&nbsp;<code>const T&amp;<\/code>&nbsp;\u4fee\u6539\u5bf9\u8c61\uff0c\u7f16\u8bd1\u5668\u4f1a\u76f4\u63a5\u62a5\u9519\u3002\u8fd9\u5b8c\u5168\u662f\u5728\u7f16\u8bd1\u5c42\u9762\u5b9e\u73b0\u7684\u7ea6\u675f\uff0c\u8fd0\u884c\u65f6\u6ca1\u6709\u4efb\u4f55\u989d\u5916\u5f00\u9500\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u5f15\u7528\u5728\u5185\u5b58\u4e2d\u7684\u8868\u73b0\u5c0f\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u65b9\u9762<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u503c\u4f20\u9012&nbsp;<code>T<\/code><\/th><th class=\"has-text-align-left\" data-align=\"left\">\u5f15\u7528\u4f20\u9012&nbsp;<code>T&amp;<\/code><\/th><th class=\"has-text-align-left\" data-align=\"left\">\u6307\u9488\u4f20\u9012&nbsp;<code>T*<\/code><\/th><\/tr><\/thead><tbody><tr><td>\u4f20\u9012\u5185\u5bb9<\/td><td>\u6574\u4e2a\u5bf9\u8c61\uff08\u6df1\u62f7\u8d1d\uff09<\/td><td>\u5bf9\u8c61\u7684\u5730\u5740\uff088\u5b57\u8282\uff09<\/td><td>\u5bf9\u8c61\u7684\u5730\u5740\uff088\u5b57\u8282\uff09<\/td><\/tr><tr><td>\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u8c03\u7528<\/td><td>\u662f<\/td><td>\u5426<\/td><td>\u5426<\/td><\/tr><tr><td>\u5f62\u53c2\u5927\u5c0f<\/td><td>\u5bf9\u8c61\u5927\u5c0f<\/td><td>\u4e00\u4e2a\u6307\u9488\u5927\u5c0f<\/td><td>\u4e00\u4e2a\u6307\u9488\u5927\u5c0f<\/td><\/tr><tr><td>\u7a7a\u503c\u5b89\u5168\u6027<\/td><td>\u4e0d\u53ef\u80fd\u4e3a\u7a7a<\/td><td>\u4e0d\u80fd\u4e3a\u7a7a\uff08\u672a\u5b9a\u4e49\u884c\u4e3a\u9664\u5916\uff09<\/td><td>\u53ef\u80fd\u4e3a&nbsp;<code>nullptr<\/code><\/td><\/tr><tr><td>\u8bed\u6cd5\u8bbf\u95ee<\/td><td>\u76f4\u63a5\u6210\u5458\u8bbf\u95ee&nbsp;<code>.<\/code><\/td><td>\u76f4\u63a5\u6210\u5458\u8bbf\u95ee&nbsp;<code>.<\/code><\/td><td>\u9700\u8981&nbsp;<code>-&gt;<\/code>&nbsp;\u6216&nbsp;<code>*<\/code><\/td><\/tr><tr><td>\u8fd0\u884c\u65f6\u5f00\u9500<\/td><td>\u62f7\u8d1d\u6210\u672c\u9ad8\uff08\u5927\u5bf9\u8c61\uff09<\/td><td>\u4e00\u6b21\u95f4\u63a5\u5bfb\u5740<\/td><td>\u4e00\u6b21\u95f4\u63a5\u5bfb\u5740<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u603b\u7ed3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u5199&nbsp;<code>void func(ss&amp; param)<\/code>&nbsp;\u5e76\u8c03\u7528&nbsp;<code>func(obj)<\/code>&nbsp;\u65f6\uff1a<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>\u4e0d\u53d1\u751f\u4efb\u4f55\u5bf9\u8c61\u62f7\u8d1d<\/strong>\uff0c<code>obj<\/code>\u00a0\u59cb\u7ec8\u5904\u4e8e\u539f\u5185\u5b58\u4f4d\u7f6e\u3002<\/li>\n\n\n\n<li>\u4ec5\u4ec5\u5c06\u00a0<code>obj<\/code>\u00a0\u7684<strong>\u5730\u5740<\/strong>\u4f20\u9012\u7ed9\u51fd\u6570\uff08\u53ef\u80fd\u901a\u8fc7\u5bc4\u5b58\u5668\uff09\uff0c\u5f62\u53c2\u00a0<code>param<\/code>\u00a0\u5b58\u50a8\u8be5\u5730\u5740\u3002<\/li>\n\n\n\n<li>\u51fd\u6570\u4f53\u5185\u5bf9\u00a0<code>param<\/code>\u00a0\u7684\u64cd\u4f5c\u901a\u8fc7<strong>\u95f4\u63a5\u5bfb\u5740<\/strong>\u76f4\u63a5\u8bfb\u5199\u00a0<code>obj<\/code>\u00a0\u7684\u5185\u5b58\uff0c\u4e0e\u4f7f\u7528\u6307\u9488\u65e0\u5f02\u3002<\/li>\n\n\n\n<li>\u5f15\u7528\u5728\u5e95\u5c42\u5c31\u662f\u201c\u81ea\u5e26\u975e\u7a7a\u4fdd\u8bc1\u548c\u89e3\u5f15\u7528\u8bed\u6cd5\u7cd6\u201d\u7684\u6307\u9488\uff0c\u5b83\u7684\u7ec8\u6781\u76ee\u7684\u662f\u5728\u63d0\u4f9b\u5b89\u5168\u4fbf\u6377\u7684\u522b\u540d\u8bed\u6cd5\u7684\u540c\u65f6\uff0c<strong>\u5b8c\u5168\u907f\u514d\u62f7\u8d1d\u5927\u5bf9\u8c61\u7684\u5f00\u9500<\/strong>\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5f15\u7528\u6210\u4e3a C++ \u4e2d\u4f20\u9012\u590d\u6742\u7c7b\u578b\u53c2\u6570\u7684\u9996\u9009\u65b9\u5f0f\u2014\u2014\u5b83\u517c\u987e\u4e86\u6548\u7387\uff08\u96f6\u62f7\u8d1d\uff09\u548c\u6613\u7528\u6027\uff08\u76f4\u63a5\u50cf\u5bf9\u8c61\u4e00\u6837\u4f7f\u7528\uff09\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-7f91984dd2e8fd17e80be8d474a74e8b wp-block-paragraph\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u80fd\u4e0d\u80fd\u8fd9\u6837\u7406\u89e3 \uff1a\u5f15\u7528\u662f\u5bf9\u6307\u9488\u7684\u5c01\u88c5\uff0c\u5f15\u7528\u50cf\u4e00\u4e2a\u53d7\u9650\u5236\u7684\u6307\u9488\uff0c\u4f20\u5165\u53c2\u6570\u662f\u5f15\u7528\uff0c\u5b9e\u9645\u4e0a\u4f20\u7684\u662f\u5730\u5740\u503c\u3002\u5982\u679c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u5f15\u7528\uff0c\u672c\u8d28\u4e0a\u8fd4\u56de\u7684\u662f\u5bf9\u4e00\u4e2a\u5730\u5740\u7684\u89e3\u5f15\u7528\u3002\u8fd9\u6837\u7406\u89e3\u5bf9\u5417\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u7684\u7406\u89e3<strong>\u5728\u5e95\u5c42\u5b9e\u73b0\u5c42\u9762\u5b8c\u5168\u6b63\u786e\uff0c\u800c\u4e14\u662f\u975e\u5e38\u7cbe\u51c6\u7684\u76f4\u89c9<\/strong>\u3002\u6211\u4eec\u628a\u5b83\u62c6\u5f00\uff0c\u4ece\u8bed\u4e49\u6807\u51c6\u3001\u7f16\u8bd1\u5668\u5b9e\u73b0\u3001\u4ee5\u53ca\u7ec6\u5fae\u5dee\u5f02\u4e09\u4e2a\u5c42\u9762\u6765\u5b8c\u5584\u8fd9\u4e2a\u6a21\u578b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f60\u8fd9\u53e5\u8bdd\u5305\u542b\u4e09\u5c42\u542b\u4e49\uff0c\u9010\u5c42\u5206\u6790\uff1a<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u201c\u5f15\u7528\u662f\u5bf9\u6307\u9488\u7684\u5c01\u88c5\uff0c\u50cf\u4e00\u4e2a\u53d7\u9650\u5236\u7684\u6307\u9488\u201d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 \u5bf9\u3002\u4ece\u5b9e\u73b0\u89d2\u5ea6\uff0c\u7f16\u8bd1\u5668\u7528\u6307\u9488\u6765\u5b9e\u73b0\u5f15\u7528\uff0c\u4f46\u589e\u52a0\u4e86<strong>\u7f16\u8bd1\u671f\u7ea6\u675f<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5fc5\u987b\u521d\u59cb\u5316\uff08\u7ed1\u5230\u5408\u6cd5\u5bf9\u8c61\uff09\uff0c\u6ca1\u6709\u201c\u7a7a\u5f15\u7528\u201d\u3002<\/li>\n\n\n\n<li>\u4e0d\u80fd\u91cd\u65b0\u7ed1\u5b9a\uff08\u76f8\u5f53\u4e8e\u00a0<code>T* const<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u65f6\u81ea\u52a8\u89e3\u5f15\u7528\uff08\u8bed\u6cd5\u7cd6\uff0c\u4e0d\u7528\u5199\u00a0<code>*<\/code>\u00a0\u6216\u00a0<code>-><\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u9650\u5236\u4f7f\u5f97\u5f15\u7528\u6bd4\u539f\u59cb\u6307\u9488\u66f4\u5b89\u5168\u3001\u66f4\u6613\u7528\uff0c\u6b63\u662f\u201c\u5c01\u88c5\u201d\u7684\u4f53\u73b0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u201c\u4f20\u5165\u53c2\u6570\u662f\u5f15\u7528\uff0c\u5b9e\u9645\u4e0a\u4f20\u7684\u662f\u5730\u5740\u503c\u201d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 \u5b8c\u5168\u6b63\u786e\u3002\u5f53\u8c03\u7528&nbsp;<code>func(obj)<\/code>&nbsp;\u4e14\u5f62\u53c2\u4e3a&nbsp;<code>T&amp;<\/code>&nbsp;\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u53d6\u51fa&nbsp;<code>obj<\/code>&nbsp;\u7684\u5730\u5740\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff08\u901a\u5e38\u901a\u8fc7\u5bc4\u5b58\u5668\uff09\uff0c\u5982\u540c\u4f20\u4e86\u4e00\u4e2a\u6307\u9488\u3002<strong>\u4e0d\u4f1a\u62f7\u8d1d\u5bf9\u8c61\u672c\u8eab<\/strong>\u3002\u8fd9\u4e0e\u4f20\u6307\u9488\u7684\u5e95\u5c42\u884c\u4e3a\u5b8c\u5168\u4e00\u81f4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u201c\u5982\u679c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u5f15\u7528\uff0c\u672c\u8d28\u4e0a\u8fd4\u56de\u7684\u662f\u5bf9\u4e00\u4e2a\u5730\u5740\u7684\u89e3\u5f15\u7528\u201d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 \u8fd9\u4e2a\u63cf\u8ff0\u6293\u4f4f\u4e86\u5b9e\u8d28\uff0c\u4f46\u201c\u89e3\u5f15\u7528\u201d\u8fd9\u4e2a\u8bcd\u9700\u8981\u7a0d\u5fae\u7406\u6e05\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u51fd\u6570\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int&amp; getElement(int* arr, size_t i) {\n    return arr[i];   <em>\/\/ \u7b49\u4ef7\u4e8e return *(arr + i);<\/em>\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e95\u5c42\u5b9e\u73b0\u4e0a\uff0c<code>return arr[i]<\/code>&nbsp;\u8fd9\u6761\u8bed\u53e5\u5e76\u4e0d\u7acb\u5373\u201c\u89e3\u5f15\u7528\u201d\u5e76\u8fd4\u56de\u6574\u6570\u503c\uff1b\u800c\u662f<strong>\u8fd4\u56de\u4e86&nbsp;<code>arr[i]<\/code>&nbsp;\u7684\u5730\u5740<\/strong>\uff08\u6216\u8005\u7b49\u4ef7\u7684\u6307\u9488\u8868\u8fbe\uff09\u3002\u8c03\u7528\u65b9\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int&amp; ref = getElement(a, 3);   <em>\/\/ ref \u88ab\u521d\u59cb\u5316\u4e3a a[3] \u7684\u522b\u540d<\/em>\nref = 10;                      <em>\/\/ \u5b9e\u9645\u4e0a\u662f\u5bf9\u90a3\u4e2a\u5730\u5740\u8d4b\u503c<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u8bd1\u5668\u751f\u6210\u4ee3\u7801\u65f6\uff0c<code>getElement<\/code>&nbsp;\u51fd\u6570\u4f1a\u628a&nbsp;<code>a + 3<\/code>&nbsp;\u8ba1\u7b97\u51fa\u7684\u5730\u5740\uff08\u6307\u9488\uff09\u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\uff08\u5b58\u5165&nbsp;<code>ref<\/code>&nbsp;\u5e95\u5c42\u5bf9\u5e94\u7684\u6307\u9488\u5b58\u50a8\u4e2d\uff09\u3002\u540e\u7eed\u5bf9&nbsp;<code>ref<\/code>&nbsp;\u7684\u8d4b\u503c&nbsp;<code>10<\/code>\uff0c\u5c31\u662f\u4e00\u6b21\u95f4\u63a5\u5bfb\u5740\u5199\u5165\u3002\u6240\u4ee5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8fd4\u56de\u503c\u662f\u5f15\u7528<\/strong>\uff1a\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5bf9\u8c61\u7684\u5730\u5740\uff08\u50cf\u8fd4\u56de\u6307\u9488\uff09\u3002<\/li>\n\n\n\n<li><strong>\u63a5\u6536\u65b9\u4f7f\u7528\u65f6<\/strong>\uff1a\u7f16\u8bd1\u5668\u81ea\u52a8\u52a0\u4e0a\u201c\u89e3\u5f15\u7528\u201d\u64cd\u4f5c\uff0c\u8ba9\u4f60\u50cf\u666e\u901a\u53d8\u91cf\u4e00\u6837\u76f4\u63a5\u64cd\u4f5c\u90a3\u4e2a\u5730\u5740\u6307\u5411\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u201c\u8fd4\u56de\u7684\u662f\u5bf9\u4e00\u4e2a\u5730\u5740\u7684\u89e3\u5f15\u7528\u201d\u8fd9\u53e5\u8bdd\u66f4\u50cf\u5728\u63cf\u8ff0\u8c03\u7528\u65b9\u7684\u611f\u53d7\uff1a\u4f60\u62ff\u5230\u4e86\u4e00\u4e2a\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u522b\u540d\uff0c\u5b83\u4eff\u4f5b\u5c31\u662f\u90a3\u4e2a\u5730\u5740\u89e3\u5f15\u7528\u540e\u7684\u5b9e\u4f53\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c<strong>\u5f15\u7528\u672c\u8eab\u5b58\u50a8\u7684\u5c31\u662f\u5730\u5740\uff0c\u89e3\u5f15\u7528\u7684\u52a8\u4f5c\u53d1\u751f\u5728\u6bcf\u6b21\u4f7f\u7528\u5f15\u7528\u65f6<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7528\u6307\u9488\u6a21\u62df\u6765\u5de9\u56fa\u4f60\u7684\u6a21\u578b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u7684\u7406\u89e3\u53ef\u4ee5\u5b8c\u7f8e\u5bf9\u5e94\u5230\u6307\u9488\u7684\u7b49\u4ef7\u5199\u6cd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u5f15\u7528\u64cd\u4f5c<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u7b49\u4ef7\u7684\u6307\u9488\u6a21\u62df<\/th><\/tr><\/thead><tbody><tr><td><code>void f(T&amp; p)<\/code><\/td><td><code>void f(T* const p)<\/code><\/td><\/tr><tr><td><code>f(obj)<\/code><\/td><td><code>f(&amp;obj)<\/code><\/td><\/tr><tr><td><code>p.x<\/code>&nbsp;\u5728\u51fd\u6570\u5185<\/td><td><code>p-&gt;x<\/code><\/td><\/tr><tr><td><code>T&amp; g() { return data_; }<\/code><\/td><td><code>T* g() { return &amp;data_; }<\/code><\/td><\/tr><tr><td><code>T&amp; r = g();<\/code><\/td><td><code>T* const r = g();<\/code>\uff08\u6ce8\u610f&nbsp;<code>r<\/code>&nbsp;\u662f\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684\u6307\u9488\uff09<\/td><\/tr><tr><td><code>r.x<\/code><\/td><td><code>r-&gt;x<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f60\u7684\u76f4\u89c9\u6b63\u662f C++ \u7f16\u8bd1\u5668\u7684\u6838\u5fc3\u5b9e\u73b0\u601d\u60f3<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u9700\u8981\u8b66\u60d5\u7684\u7ec6\u5fae\u5dee\u5f02\uff08\u6807\u51c6\u8bed\u4e49 vs \u5b9e\u73b0\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c3d\u7ba1\u5e95\u5c42\u76f8\u4f3c\uff0c\u4f46 C++ \u6807\u51c6\u8d4b\u4e88\u5f15\u7528\u4e00\u4e9b\u6307\u9488\u4e0d\u5177\u5907\u7684\u8bed\u4e49\u7279\u6027\uff0c\u5728\u5c11\u6570\u573a\u5408\u4f1a\u4ea7\u751f\u533a\u522b\uff1a<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>\u5f15\u7528\u4e0d\u662f\u5bf9\u8c61\uff0c\u4e0d\u5fc5\u7136\u5360\u7528\u5b58\u50a8<\/strong><br>\u6807\u51c6\u8bf4\u5f15\u7528\u662f\u4e00\u4e2a\u522b\u540d\uff0c\u4e0d\u8981\u6c42\u6709\u5b58\u50a8\u5730\u5740\u3002\u800c\u6307\u9488\u672c\u8eab\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5360\u6709\u5b58\u50a8\u3002\u4f46\u5728\u5b9e\u9645\u673a\u5668\u7801\u4e2d\uff0c\u5f53\u5f15\u7528\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u6216\u8fd4\u56de\u503c\u65f6\uff0c\u901a\u5e38\u9700\u8981\u4f20\u9012\u5730\u5740\uff0c\u6240\u4ee5\u5e95\u5c42\u4f1a\u5206\u914d\u6307\u9488\u5b58\u50a8\u3002\u4f46\u5bf9\u4e8e\u7eaf\u5c40\u90e8\u5f15\u7528\uff08\u5982\u00a0<code>int&amp; r = x;<\/code>\uff09\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u5b8c\u5168\u4f18\u5316\u6389\uff0c\u4e0d\u8ba9\u00a0<code>r<\/code>\u00a0\u5360\u636e\u989d\u5916\u5185\u5b58\uff0c\u800c\u6307\u9488\u53d8\u91cf\u4e00\u822c\u4f1a\u5360\u7528\u7a7a\u95f4\uff08\u9664\u975e\u4f18\u5316\u6389\uff09\u3002\u8fd9\u4f53\u73b0\u4e86\u201c\u5f15\u7528\u66f4\u50cf\u522b\u540d\u201d\u7684\u8bed\u4e49\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u80fd\u6709\u7a7a\u5f15\u7528\uff0c\u6ca1\u6709\u201c\u5f15\u7528\u7684\u5f15\u7528\u201d\uff0c\u6ca1\u6709\u6307\u9488\u7b97\u672f<\/strong><br>\u5f15\u7528\u5f3a\u5236\u5b89\u5168\uff0c\u7981\u6b62\u5371\u9669\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong><code>sizeof<\/code>\u00a0\u542b\u4e49\u4e0d\u540c<\/strong><br><code>sizeof(\u5f15\u7528)<\/code>\u00a0\u8fd4\u56de\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u5927\u5c0f\uff0c\u800c\u4e0d\u662f\u6307\u9488\u5927\u5c0f\u3002<\/li>\n\n\n\n<li><strong>\u751f\u547d\u5468\u671f\u5ef6\u957f\uff08const \u5f15\u7528\u7ed1\u5b9a\u4e34\u65f6\u5bf9\u8c61\uff09<\/strong><br>\u5f53\u4e00\u4e2a\u00a0<code>const T&amp;<\/code>\u00a0\u7ed1\u5b9a\u5230\u53f3\u503c\uff0c\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u671f\u4f1a\u5ef6\u957f\u81f3\u5f15\u7528\u7684\u4f5c\u7528\u57df\u7ed3\u675f\u3002\u8fd9\u662f\u5355\u7eaf\u7684\u6307\u9488\u505a\u4e0d\u5230\u7684\u8bed\u8a00\u9b54\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u53f3\u503c\u5f15\u7528\u4e0e\u79fb\u52a8\u8bed\u4e49<\/strong><br>\u8fd9\u662f\u5f15\u7528\u4f53\u7cfb\u7684\u91cd\u8981\u6269\u5c55\uff0c\u4e0d\u80fd\u7b80\u5355\u7b49\u4ef7\u4e8e\u6307\u9488\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ed3\u8bba<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f60\u7684\u7406\u89e3\u5b8c\u5168\u53ef\u4ee5\u4f5c\u4e3a\u5b66\u4e60\u548c\u63a8\u65ad\u5f15\u7528\u884c\u4e3a\u7684\u6838\u5fc3\u6a21\u578b\uff1a<\/strong><\/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\">\u5f15\u7528 \u2248 \u53d7\u9650\u5236\u7684\u3001\u81ea\u5e26\u89e3\u5f15\u7528\u8bed\u6cd5\u7cd6\u7684\u6307\u9488\u3002<br>\u4f20\u5f15\u7528\u5c31\u662f\u4f20\u5730\u5740\uff1b\u8fd4\u56de\u5f15\u7528\u5c31\u662f\u8fd4\u56de\u5730\u5740\uff0c\u8c03\u7528\u65b9\u81ea\u52a8\u89e3\u5f15\u7528\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u6709\u5f53\u4f60\u9047\u5230\u5f15\u7528\u7684\u201c\u9b54\u6cd5\u884c\u4e3a\u201d\uff08\u751f\u547d\u5468\u671f\u5ef6\u957f\u3001\u53f3\u503c\u5f15\u7528\u6298\u53e0\u3001\u6a21\u677f\u63a8\u5bfc\u7b49\uff09\u65f6\uff0c\u624d\u9700\u8981\u8df3\u51fa\u8fd9\u4e2a\u6a21\u578b\uff0c\u601d\u8003\u6807\u51c6\u89c4\u5b9a\u7684\u7279\u6b8a\u8bed\u4e49\u3002\u5728\u65e5\u5e38\u53c2\u6570\u8bbe\u8ba1\u3001\u8fd4\u56de\u503c\u548c\u57fa\u672c\u4f7f\u7528\u4e2d\uff0c\u8fd9\u4e2a\u201c\u6307\u9488\u5c01\u88c5\u201d\u7684\u89c6\u89d2\u51c6\u786e\u4e14\u9ad8\u6548\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-7e18e1b0a0b204ff2722aff355a0dff1 wp-block-paragraph\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u6df1\u5165\u7406\u89e3 C++ \u5f15\u7528\uff1a\u4ece\u5e95\u5c42\u539f\u7406\u5230\u6700\u4f73\u5b9e\u8df5<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">C++ \u4e2d\u7684\u5f15\u7528\uff08reference\uff09\u5e38\u5e38\u88ab\u7b80\u5355\u5730\u89e3\u91ca\u4e3a\u201c\u53d8\u91cf\u7684\u522b\u540d\u201d\u3002\u8fd9\u4e00\u5b9a\u4e49\u867d\u7136\u51c6\u786e\uff0c\u5374\u5f88\u96be\u8ba9\u5b66\u4e60\u8005\u771f\u6b63\u7406\u89e3\u5f15\u7528\u7684\u884c\u4e3a\u3001\u4ee3\u4ef7\u548c\u5a01\u529b\u3002\u672c\u6587\u5c1d\u8bd5\u4ece\u5e95\u5c42\u5b9e\u73b0\u673a\u5236\u51fa\u53d1\uff0c\u7ed3\u5408\u5e38\u89c1\u4f7f\u7528\u573a\u666f\u548c\u4e0e\u6307\u9488\u7684\u5bf9\u6bd4\uff0c\u4e3a\u4f60\u6784\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684 C++ \u5f15\u7528\u77e5\u8bc6\u4f53\u7cfb\u3002\u9605\u8bfb\u4e4b\u540e\uff0c\u4f60\u4e0d\u4ec5\u80fd\u719f\u7ec3\u4f7f\u7528\u5f15\u7528\uff0c\u8fd8\u80fd\u5411\u4ed6\u4eba\u6e05\u6670\u89e3\u91ca\u201c\u5f15\u7528\u5230\u5e95\u662f\u4ec0\u4e48\u201d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u4e3a\u4ec0\u4e48\u9700\u8981\u5f15\u7528\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 C \u8bed\u8a00\u4e2d\uff0c\u5982\u679c\u60f3\u5728\u51fd\u6570\u5185\u4fee\u6539\u5916\u90e8\u53d8\u91cf\uff0c\u5fc5\u987b\u4f20\u6307\u9488\uff1b\u5982\u679c\u60f3\u907f\u514d\u590d\u5236\u5927\u7ed3\u6784\u4f53\uff0c\u4e5f\u5fc5\u987b\u4f20\u6307\u9488\u3002\u6307\u9488\u662f\u552f\u4e00\u7684\u95f4\u63a5\u8bbf\u95ee\u5de5\u5177\uff0c\u4f46\u5b83\u5e26\u6709\u539f\u59cb\u7684\u7c97\u7cd9\u611f\uff1a\u53ef\u4ee5\u4e3a\u7a7a\u3001\u53ef\u4ee5\u4fee\u6539\u6307\u5411\u3001\u5fc5\u987b\u663e\u5f0f\u89e3\u5f15\u7528\u3002C++ \u7684\u8bbe\u8ba1\u8005\u4eec\u5e0c\u671b\u63d0\u4f9b\u4e00\u79cd<strong>\u66f4\u5b89\u5168\u3001\u66f4\u81ea\u7136<\/strong>\u7684\u95f4\u63a5\u8bbf\u95ee\u673a\u5236\u2014\u2014\u5f15\u7528\u7531\u6b64\u8bde\u751f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f15\u7528\u7684\u6838\u5fc3\u4ef7\u503c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u62f7\u8d1d<\/strong>\uff1a\u4f20\u5f15\u7528\u53ea\u4f20\u5730\u5740\uff0c\u65e0\u8bba\u5bf9\u8c61\u591a\u5927\uff0c\u5f00\u9500\u6052\u5b9a\u3002<\/li>\n\n\n\n<li><strong>\u5141\u8bb8\u4fee\u6539\u5b9e\u53c2<\/strong>\uff1a\u975e const \u5f15\u7528\u8ba9\u51fd\u6570\u76f4\u63a5\u64cd\u4f5c\u5916\u90e8\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>\u8bed\u6cd5\u7b80\u6d01<\/strong>\uff1a\u4f7f\u7528\u5f15\u7528\u5c31\u50cf\u4f7f\u7528\u539f\u5bf9\u8c61\uff0c\u65e0\u9700\u00a0<code>*<\/code>\u00a0\u548c\u00a0<code>-><\/code>\u3002<\/li>\n\n\n\n<li><strong>\u5f3a\u5236\u975e\u7a7a<\/strong>\uff1a\u5f15\u7528\u5fc5\u987b\u7ed1\u5b9a\u5230\u5408\u6cd5\u5bf9\u8c61\uff0c\u4e0d\u80fd\u5b58\u5728\u201c\u7a7a\u5f15\u7528\u201d\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u5f15\u7528\u662f\u4ec0\u4e48\uff1a\u522b\u540d\u80cc\u540e\u7684\u6307\u9488<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u8bed\u8a00\u6807\u51c6\u4e0a\u8bf4\uff0c\u5f15\u7528\u662f<strong>\u5bf9\u8c61\u7684\u522b\u540d<\/strong>\uff0c\u5b83\u4e0d\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u5bf9\u8c61\uff0c\u4e0d\u5360\u6709\u5b58\u50a8\uff08\u7406\u8bba\u4e0a\uff09\u3002\u4f46\u5728\u5b9e\u9645\u7f16\u8bd1\u5668\u7684\u5b9e\u73b0\u4e2d\uff0c<strong>\u5f15\u7528\u5728\u5e95\u5c42\u901a\u5e38\u7528\u6307\u9488\u6765\u5b9e\u73b0<\/strong>\u3002\u53ef\u4ee5\u628a\u5f15\u7528\u7406\u89e3\u6210\u4e00\u4e2a\u201c\u88ab\u5c01\u5370\u7684\u6307\u9488\u201d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b83\u5b58\u50a8\u4e86\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u5730\u5740\uff08\u5982\u540c\u6307\u9488\uff09\u3002<\/li>\n\n\n\n<li>\u5b83\u5fc5\u987b\u5728\u5b9a\u4e49\u65f6\u7acb\u5373\u521d\u59cb\u5316\uff0c\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5408\u6cd5\u5bf9\u8c61\uff08\u4e0d\u80fd\u4e3a\u7a7a\uff09\u3002<\/li>\n\n\n\n<li>\u7ed1\u5b9a\u5b8c\u6210\u540e\uff0c<strong>\u4e0d\u80fd\u91cd\u65b0\u7ed1\u5b9a<\/strong>\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\uff08\u6307\u9488\u672c\u8eab\u662f\u00a0<code>const<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u5f15\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u81ea\u52a8\u5e2e\u6211\u4eec\u89e3\u5f15\u7528\uff08\u8bed\u6cd5\u7cd6\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u7528\u6307\u9488\u6765\u6a21\u62df\u5f15\u7528\uff0c\u5b83\u4eec\u7684\u5173\u7cfb\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u5f15\u7528\u5199\u6cd5<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u7b49\u4ef7\u7684\u6307\u9488\u6a21\u62df<\/th><\/tr><\/thead><tbody><tr><td><code>T&amp; ref = obj;<\/code><\/td><td><code>T* const ref = &amp;obj;<\/code><\/td><\/tr><tr><td><code>ref.member<\/code><\/td><td><code>ref-&gt;member<\/code><\/td><\/tr><tr><td><code>ref = value;<\/code><\/td><td><code>*ref = value;<\/code><\/td><\/tr><tr><td><code>T&amp; func() { return obj; }<\/code><\/td><td><code>T* func() { return &amp;obj; }<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u6a21\u62df\u51e0\u4e4e\u5b8c\u7f8e\u5730\u63ed\u793a\u4e86\u5f15\u7528\u7684\u5e95\u5c42\u884c\u4e3a\u3002\u4f46\u8bf7\u8bb0\u4f4f\uff0c\u5f15\u7528\u5728 C++ \u8bed\u4e49\u4e0a\u5e76\u975e\u6307\u9488\uff0c\u5b83\u6709\u4e00\u4e9b\u6307\u9488\u4e0d\u5177\u5907\u7684\u8bed\u8a00\u7279\u6027\uff08\u5982\u4e34\u65f6\u5bf9\u8c61\u751f\u547d\u5468\u671f\u5ef6\u957f\u3001\u53f3\u503c\u5f15\u7528\u7b49\uff09\uff0c\u56e0\u6b64\u8fd9\u4e2a\u6a21\u578b\u4e3b\u8981\u7528\u4e8e<strong>\u7406\u89e3\u5b9e\u73b0<\/strong>\uff0c\u7f16\u5199\u4ee3\u7801\u65f6\u8fd8\u662f\u5e94\u8be5\u7528\u201c\u522b\u540d\u201d\u601d\u7ef4\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u5f15\u7528\u7684\u5206\u7c7b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++ \u7684\u5f15\u7528\u5206\u4e3a\u4e09\u5927\u7c7b\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u5de6\u503c\u5f15\u7528&nbsp;<code>T&amp;<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u4f20\u7edf\u7684\u5f15\u7528\uff0c\u7ed1\u5b9a\u5230\u5de6\u503c\uff08\u6709\u540d\u5b57\u3001\u53ef\u53d6\u5730\u5740\u7684\u5bf9\u8c61\uff09\u3002\u53ef\u8bfb\u53ef\u5199\uff0c\u5e38\u7528\u4e8e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4fee\u6539\u5916\u90e8\u5b9e\u53c2<\/li>\n\n\n\n<li>\u8fd4\u56de\u53ef\u4fee\u6539\u7684\u6210\u5458<\/li>\n\n\n\n<li>\u94fe\u5f0f\u8c03\u7528<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 const \u5de6\u503c\u5f15\u7528&nbsp;<code>const T&amp;<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u80fd\u8bfb\u4e0d\u80fd\u5199\u7684\u5de6\u503c\u5f15\u7528\u3002\u5b83\u6709\u4e00\u9879\u201c\u8d85\u80fd\u529b\u201d\uff1a<strong>\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53f3\u503c\uff08\u4e34\u65f6\u5bf9\u8c61\uff09\u5e76\u5ef6\u957f\u5176\u751f\u547d\u5468\u671f<\/strong>\u3002\u8fd9\u4f7f\u5f97&nbsp;<code>const T&amp;<\/code>&nbsp;\u6210\u4e3a\u53ea\u8bfb\u53c2\u6570\u4f20\u9012\u7684\u4e0d\u4e8c\u4e4b\u9009\uff0c\u65e2\u80fd\u63a5\u53d7\u5de6\u503c\uff0c\u4e5f\u80fd\u63a5\u53d7\u4e34\u65f6\u5bf9\u8c61\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 \u53f3\u503c\u5f15\u7528&nbsp;<code>T&amp;&amp;<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C++11 \u5f15\u5165\uff0c\u7ed1\u5b9a\u5230\u53f3\u503c\uff08\u5373\u5c06\u9500\u6bc1\u7684\u4e34\u65f6\u5bf9\u8c61\uff09\uff0c\u7528\u6765\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u548c\u5b8c\u7f8e\u8f6c\u53d1\u3002\u672c\u6587\u4e3b\u8981\u8ba8\u8bba\u524d\u4e24\u79cd\uff0c\u53f3\u503c\u5f15\u7528\u5c06\u5728\u540e\u7eed\u6587\u7ae0\u4e2d\u4e13\u9898\u8bb2\u89e3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u5f15\u7528\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\uff08\u91cd\u5934\u620f\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 \u5f15\u7528\u4f5c\u4e3a\u51fd\u6570\u5f62\u53c2<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">struct BigData { <em>\/* \u5927\u91cf\u6210\u5458\uff0c\u53ef\u80fd\u5360\u7528\u6570\u767e\u5b57\u8282 *\/<\/em> };\nvoid process(BigData&amp; data) { data.x = 10; }\n\nint main() {\n    BigData obj;\n    process(obj);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u7528&nbsp;<code>process(obj)<\/code>&nbsp;\u65f6\u53d1\u751f\u4e86\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6ca1\u6709\u53d1\u751f\u4efb\u4f55\u5bf9\u8c61\u62f7\u8d1d<\/strong>\u3002\u7f16\u8bd1\u5668\u4e0d\u4f1a\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4e5f\u4e0d\u4f1a\u5728\u6808\u4e0a\u521b\u5efa\u00a0<code>BigData<\/code>\u00a0\u7684\u526f\u672c\u3002<\/li>\n\n\n\n<li>\u4f20\u9012\u7684\u662f\u00a0<strong><code>obj<\/code>\u00a0\u7684\u8d77\u59cb\u5730\u5740<\/strong>\u3002\u5728 x86-64 \u4f53\u7cfb\u4e0b\uff0c\u8fd9\u4e2a\u5730\u5740\u901a\u5e38\u901a\u8fc7\u5bc4\u5b58\u5668\uff08\u5982\u00a0<code>RDI<\/code>\uff09\u76f4\u63a5\u4f20\u9012\uff0c\u5f00\u9500\u4ec5\u4e3a\u4e00\u4e2a 8 \u5b57\u8282\u6307\u9488\u3002<\/li>\n\n\n\n<li><code>process<\/code>\u00a0\u51fd\u6570\u5185\u90e8\uff0c\u5f62\u53c2\u00a0<code>data<\/code>\u00a0\u5728\u6982\u5ff5\u4e0a\u5360\u636e\u4e00\u4e2a\u6307\u9488\u5927\u5c0f\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u5b58\u50a8\u7684\u5c31\u662f\u00a0<code>obj<\/code>\u00a0\u7684\u5730\u5740\u3002<\/li>\n\n\n\n<li>\u5f53\u5199\u4e0b\u00a0<code>data.x = 10<\/code>\u00a0\u65f6\uff0c\u7f16\u8bd1\u5668\u751f\u6210\u7c7b\u4f3c\u00a0<code>mov [rax + offset], 10<\/code>\u00a0\u7684\u6307\u4ee4\uff0c\u901a\u8fc7\u5730\u5740\u95f4\u63a5\u4fee\u6539\u00a0<code>obj<\/code>\u00a0\u7684\u6210\u5458\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6574\u4e2a\u8fc7\u7a0b\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void process(BigData* const data) { data-&gt;x = 10; }\nint main() {\n    BigData obj;\n    process(&amp;obj);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u5f15\u7528\u8ba9\u4f60\u514d\u4e8e\u5199&nbsp;<code>&amp;<\/code>&nbsp;\u548c&nbsp;<code>-&gt;<\/code>\uff0c\u4e14\u65e0\u6cd5\u5728\u51fd\u6570\u5185\u6539\u53d8&nbsp;<code>data<\/code>&nbsp;\u7684\u6307\u5411\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 \u5f15\u7528\u4f5c\u4e3a\u8fd4\u56de\u503c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8003\u8651\u8fd4\u56de\u6210\u5458\u5f15\u7528\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Container {\n    int value_;\npublic:\n    int&amp; get() { return value_; }\n};\n\nint main() {\n    Container c;\n    int&amp; r = c.get();\n    r = 42; <em>\/\/ \u4fee\u6539\u7684\u662f c.value_<\/em>\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>get()<\/code>\u00a0\u51fd\u6570\u8fd4\u56de\u00a0<code>int&amp;<\/code>\uff0c\u5e95\u5c42\u5b9e\u73b0\u4e0a\u5b83\u8fd4\u56de\u7684\u662f\u00a0<code>value_<\/code>\u00a0\u7684\u5730\u5740\uff08\u5373\u00a0<code>return &amp;value_;<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u8c03\u7528\u65b9\u00a0<code>int&amp; r = c.get();<\/code>\u00a0\u5185\u90e8\uff0c<code>r<\/code>\u00a0\u5b58\u50a8\u7684\u662f\u00a0<code>c.value_<\/code>\u00a0\u7684\u5730\u5740\u3002<\/li>\n\n\n\n<li>\u4e4b\u540e\u5bf9\u00a0<code>r<\/code>\u00a0\u7684\u4efb\u4f55\u64cd\u4f5c\uff08\u5982\u00a0<code>r = 42<\/code>\uff09\uff0c\u7f16\u8bd1\u5668\u90fd\u4f1a\u81ea\u52a8\u89e3\u5f15\u7528\uff0c\u5c06 42 \u5199\u5165\u00a0<code>c.value_<\/code>\u00a0\u7684\u5185\u5b58\u4f4d\u7f6e\u3002\u8fd9\u91cc\u4e0d\u5b58\u5728\u201c\u5bf9\u5f15\u7528\u672c\u8eab\u8d4b\u503c\u201d\u8fd9\u4e00\u6982\u5ff5\uff0c\u56e0\u4e3a\u5f15\u7528\u4e0d\u662f\u72ec\u7acb\u7684\u5bf9\u8c61\uff0c\u6240\u6709\u64cd\u4f5c\u90fd\u88ab\u8f6c\u53d1\u5230\u539f\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 \u5f15\u7528\u7684\u201c\u81ea\u52a8\u89e3\u5f15\u7528\u201d\u673a\u5236<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u7406\u89e3\u5f15\u7528\u884c\u4e3a\u6700\u5173\u952e\u7684\u4e00\u73af\u3002\u5f53\u4f60\u5199\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int a = 100;\nint&amp; ref = a;\nref = 200;   <em>\/\/ \u2460<\/em>\nint b = ref; <em>\/\/ \u2461<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bed\u53e5\u2460\u5e76\u975e\u4fee\u6539&nbsp;<code>ref<\/code>&nbsp;\u81ea\u8eab\uff08\u5b83\u5b58\u50a8\u7684\u5730\u5740\u5e76\u6ca1\u6709\u53d8\u6210 200\uff09\uff0c\u800c\u662f\u7f16\u8bd1\u5668\u81ea\u52a8\u5c06&nbsp;<code>ref = 200<\/code>&nbsp;\u8f6c\u6362\u4e3a&nbsp;<code>*pointer_to_a = 200<\/code>\uff0c\u5373\u4fee\u6539\u53d8\u91cf&nbsp;<code>a<\/code>\u3002<br>\u8bed\u53e5\u2461\u4e2d\uff0c<code>ref<\/code>&nbsp;\u51fa\u73b0\u5728\u8d4b\u503c\u8fd0\u7b97\u7b26\u53f3\u4fa7\uff0c\u5b83\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u65f6\uff0c\u7c7b\u578b\u4e3a&nbsp;<code>int<\/code>\uff08\u5de6\u503c\uff09\uff0c\u7f16\u8bd1\u5668\u8bfb\u53d6&nbsp;<code>a<\/code>&nbsp;\u7684\u503c\u8d4b\u7ed9&nbsp;<code>b<\/code>\u3002<strong>\u4efb\u4f55\u4f7f\u7528\u5f15\u7528\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\uff0c\u90fd\u4f1a\u7acb\u5373\u66ff\u6362\u4e3a\u88ab\u5f15\u7528\u5bf9\u8c61\u672c\u8eab<\/strong>\uff0c\u5305\u62ec\u5b83\u7684\u7c7b\u578b\u548c\u503c\u7c7b\u522b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\uff0c\u4e0b\u9762\u4ee3\u7801\u5b8c\u5168\u5408\u6cd5\u4e14\u6b63\u5e38\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int a = 0;\nint&amp; b = a;\nb = 10;   <em>\/\/ \u7b49\u4ef7\u4e8e a = 10;<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u201c\u5bf9\u5f15\u7528\u8d4b\u503c\u201d\u4ece\u4e0d\u4f1a\u62a5\u9519\u2014\u2014\u56e0\u4e3a\u6839\u672c\u4e0d\u5b58\u5728\u201c\u5bf9\u5f15\u7528\u672c\u8eab\u8d4b\u503c\u201d\u8fd9\u4e2a\u64cd\u4f5c\uff0c\u5b83\u6c38\u8fdc\u662f\u5bf9\u539f\u5bf9\u8c61\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 \u5f15\u7528\u5230\u5e95\u5360\u4e0d\u5360\u5185\u5b58\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u4e49\u4e0a<\/strong>\uff1a\u5f15\u7528\u4e0d\u662f\u5bf9\u8c61\uff0c\u4e0d\u8981\u6c42\u6709\u72ec\u7acb\u5b58\u50a8\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u73b0\u4e0a<\/strong>\uff1a\u5f53\u5f15\u7528\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u6216\u8fd4\u56de\u503c\u65f6\uff0c\u5730\u5740\u5fc5\u987b\u4f20\u9012\uff0c\u5e95\u5c42\u5fc5\u7136\u6709\u4e00\u4e2a\u6307\u9488\u5927\u5c0f\u7684\u5b58\u50a8\u5355\u5143\uff08\u5bc4\u5b58\u5668\u6216\u6808\u7a7a\u95f4\uff09\u3002<\/li>\n\n\n\n<li><strong>\u4f18\u5316\u65f6<\/strong>\uff1a\u5bf9\u4e8e\u5c40\u90e8\u7684\u3001\u4ec5\u5145\u5f53\u522b\u540d\u7684\u5f15\u7528\uff08\u5982\u00a0<code>int&amp; r = x;<\/code>\uff09\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u5b8c\u5168\u4f18\u5316\u6389\uff0c\u8ba9\u00a0<code>r<\/code>\u00a0\u4e0d\u5360\u7528\u989d\u5916\u5185\u5b58\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u7528&nbsp;<code>sizeof<\/code>&nbsp;\u9a8c\u8bc1\uff1a<code>sizeof(r)<\/code>&nbsp;\u8fd4\u56de\u7684\u662f\u88ab\u5f15\u7528\u7c7b\u578b\u7684\u5927\u5c0f\uff0c\u800c\u4e0d\u662f\u6307\u9488\u5927\u5c0f\u3002\u8fd9\u4e5f\u662f\u5f15\u7528\u4e0e\u6307\u9488\u7684\u663e\u8457\u533a\u522b\u4e4b\u4e00\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u5f15\u7528\u4f7f\u7528\u7684\u5178\u578b\u573a\u666f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 \u907f\u514d\u62f7\u8d1d\u5927\u5bf9\u8c61\uff08const \u5f15\u7528\u53c2\u6570\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void analyze(const BigMatrix&amp; m); <em>\/\/ \u96f6\u62f7\u8d1d\uff0c\u53ea\u8bfb\u8bbf\u95ee<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f C++ \u4e2d\u6700\u5e38\u89c1\u7684\u5f15\u7528\u7528\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 \u4fee\u6539\u5b9e\u53c2\uff08\u975e const \u5f15\u7528\u53c2\u6570\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void swap(int&amp; a, int&amp; b) { int t = a; a = b; b = t; }<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.3 \u8fd4\u56de\u5de6\u503c\uff0c\u652f\u6301\u8d4b\u503c\u8bed\u4e49<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int&amp; operator[](size_t i) { return data[i]; }\nvec[0] = 10; <em>\/\/ \u901a\u8fc7\u5f15\u7528\u8d4b\u503c<\/em><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.4 \u8303\u56f4 for \u5faa\u73af\u4e2d\u4fee\u6539\u6216\u53ea\u8bfb\u904d\u5386<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for (auto&amp; x : vec) x *= 2;       <em>\/\/ \u4fee\u6539<\/em>\nfor (const auto&amp; x : vec) { ... } <em>\/\/ \u53ea\u8bfb\uff0c\u4e14\u907f\u514d\u62f7\u8d1d<\/em><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.5 \u591a\u6001\u57fa\u7c7b\u5f15\u7528<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void draw(const Shape&amp; s) { s.paint(); } <em>\/\/ \u52a8\u6001\u7ed1\u5b9a\uff0c\u65e0\u5bf9\u8c61\u5207\u7247<\/em><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. const \u5f15\u7528\u6df1\u5165<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">6.1 \u4e3a\u4ec0\u4e48&nbsp;<code>const T&amp;<\/code>&nbsp;\u53ef\u4ee5\u7ed1\u5b9a\u53f3\u503c\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const std::string&amp; ref = getString(); <em>\/\/ getString \u8fd4\u56de\u4e34\u65f6 string<\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u975e const \u5de6\u503c\u5f15\u7528\u4e0d\u80fd\u7ed1\u5b9a\u53f3\u503c\uff0c\u56e0\u4e3a\u53f3\u503c\u901a\u5e38\u6ca1\u6709\u6301\u4e45\u7684\u5185\u5b58\u5730\u5740\uff0c\u4fee\u6539\u5b83\u610f\u4e49\u4e0d\u5927\u4e14\u5371\u9669\u3002\u800c&nbsp;<code>const T&amp;<\/code>&nbsp;\u88ab\u6807\u51c6\u8d4b\u4e88\u4e86\u201c\u5ef6\u957f\u4e34\u65f6\u5bf9\u8c61\u751f\u547d\u5468\u671f\u201d\u7684\u80fd\u529b\uff1a\u4e34\u65f6\u5bf9\u8c61\u4f1a\u4e00\u76f4\u5b58\u6d3b\u5230\u8be5 const \u5f15\u7528\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u8fd9\u4e00\u7279\u6027\u8ba9\u6211\u4eec\u80fd\u5b89\u5168\u5730\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u7684\u4e34\u65f6\u5bf9\u8c61\uff0c\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6.2&nbsp;<code>const T&amp;<\/code>&nbsp;\u4f5c\u4e3a\u53ea\u8bfb\u53c2\u6570\u7684\u4f18\u52bf<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b83\u80fd\u540c\u65f6\u63a5\u53d7\u5de6\u503c\u548c\u53f3\u503c\uff0c\u4e14\u660e\u786e\u627f\u8bfa\u4e0d\u4fee\u6539\u5bf9\u8c61\uff0c\u662f\u7406\u60f3\u7684\u53ea\u8bfb\u8f93\u5165\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u7edd\u5927\u591a\u6570\u975e\u57fa\u672c\u7c7b\u578b\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6.3 const \u5bf9\u8c61\u4e0e const \u6210\u5458\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u6709\u4e00\u4e2a&nbsp;<code>const Person<\/code>&nbsp;\u5bf9\u8c61\uff08\u6216&nbsp;<code>const Person&amp;<\/code>\uff09\uff0c\u4f60<strong>\u53ea\u80fd<\/strong>\u8c03\u7528\u5b83\u7684 const \u6210\u5458\u51fd\u6570\u3002\u539f\u56e0\u5728\u4e8e\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u9690\u85cf\u7684&nbsp;<code>this<\/code>&nbsp;\u6307\u9488\u7c7b\u578b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u975e const \u6210\u5458\u51fd\u6570\uff1a<code>Person* const this<\/code><\/li>\n\n\n\n<li>const \u6210\u5458\u51fd\u6570\uff1a<code>const Person* const this<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><code>const<\/code>&nbsp;\u5bf9\u8c61\u63d0\u4f9b\u7684&nbsp;<code>this<\/code>&nbsp;\u6307\u9488\u662f&nbsp;<code>const Person*<\/code>\uff0c\u4e0d\u80fd\u9690\u5f0f\u8f6c\u6362\u4e3a&nbsp;<code>Person*<\/code>\uff08\u4e22\u5f03 const \u9650\u5b9a\u7b26\uff09\uff0c\u56e0\u6b64\u65e0\u6cd5\u8c03\u7528\u975e const \u51fd\u6570\u3002\u8fd9\u6b63\u662f C++ \u5e38\u91cf\u6b63\u786e\u6027\uff08const correctness\uff09\u7684\u57fa\u77f3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Person {\n    std::string name_;\npublic:\n    const std::string&amp; getName() const { return name_; } <em>\/\/ const \u51fd\u6570<\/em>\n    void setName(const std::string&amp; n) { name_ = n; }     <em>\/\/ \u975e const \u51fd\u6570<\/em>\n};\n\nvoid print(const Person&amp; p) {\n    p.getName(); <em>\/\/ \u2705 \u53ef\u4ee5\uff0cgetName \u662f const<\/em>\n    <em>\/\/ p.setName(\"Alice\"); \/\/ \u274c \u7f16\u8bd1\u9519\u8bef<\/em>\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u5f15\u7528 vs \u6307\u9488\uff1a\u4e3a\u4ec0\u4e48\u73b0\u4ee3 C++ \u4e2d\u5f15\u7528\u591a\uff0c\u6307\u9488\u5c11\uff1f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u7ef4\u5ea6<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u6307\u9488&nbsp;<code>T*<\/code><\/th><th class=\"has-text-align-left\" data-align=\"left\">\u5f15\u7528&nbsp;<code>T&amp;<\/code><\/th><\/tr><\/thead><tbody><tr><td>\u53ef\u7a7a\u6027<\/td><td>\u53ef\u4e3a&nbsp;<code>nullptr<\/code>\uff0c\u5fc5\u987b\u68c0\u67e5<\/td><td>\u5f3a\u5236\u975e\u7a7a\uff0c\u66f4\u5b89\u5168<\/td><\/tr><tr><td>\u91cd\u65b0\u7ed1\u5b9a<\/td><td>\u53ef\u4ee5\u4fee\u6539\u6307\u5411<\/td><td>\u4e0d\u53ef\u91cd\u65b0\u7ed1\u5b9a\uff0c\u8bed\u4e49\u6e05\u6670<\/td><\/tr><tr><td>\u8bed\u6cd5<\/td><td>\u9700\u8981&nbsp;<code>*<\/code>&nbsp;\u89e3\u5f15\u7528\uff0c<code>-&gt;<\/code>&nbsp;\u8bbf\u95ee\u6210\u5458<\/td><td>\u50cf\u539f\u5bf9\u8c61\u4e00\u6837\u76f4\u63a5\u4f7f\u7528<\/td><\/tr><tr><td>\u6240\u6709\u6743<\/td><td>\u53ef\u80fd\u8868\u8fbe\u6240\u6709\u6743\uff08\u9700\u624b\u52a8\u7ba1\u7406\uff09<\/td><td>\u4ece\u4e0d\u8868\u8fbe\u6240\u6709\u6743<\/td><\/tr><tr><td>\u7b97\u672f\u64cd\u4f5c<\/td><td>\u652f\u6301&nbsp;<code>++<\/code>\u3001<code>--<\/code>&nbsp;\u7b49\u5730\u5740\u8fd0\u7b97<\/td><td>\u4e0d\u652f\u6301\uff0c\u4ec5\u5355\u4e00\u5bf9\u8c61\u522b\u540d<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 C++11 \u53ca\u4ee5\u540e\uff0c\u539f\u59cb\u6307\u9488\u7684\u804c\u8d23\u88ab\u8fdb\u4e00\u6b65\u7ec6\u5206\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6240\u6709\u6743\u7ba1\u7406<\/strong>\u00a0\u4ea4\u7ed9\u667a\u80fd\u6307\u9488\uff08<code>unique_ptr<\/code>\u3001<code>shared_ptr<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u7a7a\u4e14\u65e0\u6240\u6709\u6743\u7684\u8bbf\u95ee<\/strong>\u00a0\u5076\u5c14\u4f7f\u7528\u539f\u59cb\u6307\u9488\u3002<\/li>\n\n\n\n<li><strong>\u5fc5\u987b\u5b58\u5728\u7684\u5bf9\u8c61\u8bbf\u95ee<\/strong>\u00a0\u4e00\u5f8b\u7528\u5f15\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\uff0c\u5f15\u7528\u6210\u4e3a\u4e86\u4f20\u9012\u53c2\u6570\u3001\u8fd4\u56de\u522b\u540d\u3001\u904d\u5386\u5bb9\u5668\u7b49\u573a\u666f\u7684\u9ed8\u8ba4\u9009\u62e9\uff0c\u6781\u5927\u5730\u964d\u4f4e\u4e86\u7a7a\u6307\u9488\u548c\u8d44\u6e90\u6cc4\u6f0f\u7684\u98ce\u9669\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. \u503c\u4f20\u9012 vs \u5f15\u7528\u4f20\u9012\uff1a\u5982\u4f55\u9009\u62e9\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e76\u975e\u6240\u6709\u60c5\u51b5\u90fd\u9002\u5408\u7528\u5f15\u7528\u3002\u9009\u62e9\u4f9d\u636e\u4e3b\u8981\u662f\u5bf9\u8c61\u5927\u5c0f\u548c\u4fee\u6539\u9700\u6c42\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u53c2\u6570\u7c7b\u578b<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u63a8\u8350\u4f20\u9012\u65b9\u5f0f<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u7406\u7531<\/th><\/tr><\/thead><tbody><tr><td>\u57fa\u672c\u7c7b\u578b&nbsp;<code>int<\/code>\u3001<code>double<\/code>&nbsp;\u7b49<\/td><td><strong>\u503c\u4f20\u9012<\/strong>&nbsp;<code>T<\/code><\/td><td>\u62f7\u8d1d\u6210\u672c\u6781\u4f4e\uff0c\u751a\u81f3\u901a\u8fc7\u5bc4\u5b58\u5668\u4f18\u5316\uff1b\u5f15\u7528\u53cd\u800c\u95f4\u63a5\u5bfb\u5740<\/td><\/tr><tr><td>\u5c0f\u578b POD\uff08\u226416 \u5b57\u8282\uff09<\/td><td><strong>\u503c\u4f20\u9012<\/strong><\/td><td>\u540c\u4e0a<\/td><\/tr><tr><td>\u5927\u5bf9\u8c61\u3001\u975e\u57fa\u672c\u7c7b\u578b\uff08\u53ea\u8bfb\uff09<\/td><td><strong><code>const T&amp;<\/code><\/strong><\/td><td>\u96f6\u62f7\u8d1d\uff0c\u5b89\u5168\u6027\u9ad8<\/td><\/tr><tr><td>\u9700\u8981\u4fee\u6539\u5b9e\u53c2<\/td><td><strong><code>T&amp;<\/code><\/strong><\/td><td>\u76f4\u63a5\u4fee\u6539\u5916\u90e8\u5bf9\u8c61<\/td><\/tr><tr><td>\u51fd\u6570\u5185\u90e8\u9700\u8981\u4fdd\u6709\u526f\u672c\uff08\u5982 setter\uff09<\/td><td><strong>\u503c\u4f20\u9012<\/strong>&nbsp;<code>T<\/code>&nbsp;+&nbsp;<code>std::move<\/code><\/td><td>\u517c\u987e\u5de6\u503c\u548c\u53f3\u503c\uff0c\u6700\u4f73\u6027\u80fd<\/td><\/tr><tr><td>\u6a21\u677f\u8f6c\u53d1\u53c2\u6570<\/td><td><strong><code>T&amp;&amp;<\/code><\/strong>&nbsp;+&nbsp;<code>std::forward<\/code><\/td><td>\u5b8c\u7f8e\u8f6c\u53d1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u89c1\u8bef\u533a\uff1a<strong>\u76f2\u76ee\u5728\u6240\u6709\u53c2\u6570\u4e0a\u6dfb\u52a0&nbsp;<code>const &amp;<\/code>\uff0c\u5305\u62ec&nbsp;<code>int<\/code><\/strong>\u3002\u8fd9\u4f1a\u589e\u52a0\u95f4\u63a5\u5bfb\u5740\u5f00\u9500\uff0c\u53ef\u80fd\u53cd\u800c\u4e0d\u5982\u503c\u4f20\u9012\u5feb\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. \u5e38\u89c1\u9677\u9631<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">9.1 \u60ac\u7a7a\u5f15\u7528\uff08Dangling Reference\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7edd\u5bf9\u4e0d\u8981\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf\u7684\u5f15\u7528\uff01<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const std::string&amp; create() {\n    std::string local = \"hello\";\n    return local; <em>\/\/ \u274c \u51fd\u6570\u8fd4\u56de\u540e local \u88ab\u9500\u6bc1\uff0c\u5f15\u7528\u60ac\u7a7a<\/em>\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">9.2 \u8fd4\u56de\u975e const \u5f15\u7528\u7834\u574f\u5c01\u88c5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Account {\n    double balance;\npublic:\n    double&amp; getBalance() { return balance; } <em>\/\/ \u5916\u90e8\u53ef\u76f4\u63a5\u4fee\u6539\u4f59\u989d\uff01<\/em>\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e94\u8fd4\u56de&nbsp;<code>const double&amp;<\/code>&nbsp;\u6216\u63d0\u4f9b\u4e13\u95e8\u7684 setter\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9.3 const \u6210\u5458\u51fd\u6570\u8fd4\u56de\u975e const \u6307\u9488\/\u5f15\u7528\u6307\u5411\u5185\u90e8\u6570\u636e<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">cpp<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Container {\n    int* p;\npublic:\n    int* get() const { return p; } <em>\/\/ \u867d\u51fd\u6570 const\uff0c\u4f46\u5916\u90e8\u53ef\u901a\u8fc7\u6307\u9488\u4fee\u6539 *p<\/em>\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e94\u8fd4\u56de&nbsp;<code>const int*<\/code>\uff0c\u4fdd\u6301 const \u6b63\u786e\u6027\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. \u603b\u7ed3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f15\u7528\u4e0d\u662f\u9b54\u6cd5\uff0c\u5b83\u662f\u5728\u6307\u9488\u57fa\u7840\u4e0a\u65bd\u52a0\u4e86\u4e25\u683c\u7684\u7f16\u8bd1\u671f\u7ea6\u675f\u548c\u4eba\u6027\u5316\u7684\u8bed\u6cd5\u7cd6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5e95\u5c42<\/strong>\uff1a\u5b58\u50a8\u5730\u5740\uff0c\u4f20\u9012\u5730\u5740\uff0c\u81ea\u52a8\u89e3\u5f15\u7528\u3002<\/li>\n\n\n\n<li><strong>\u8bed\u4e49<\/strong>\uff1a\u522b\u540d\uff0c\u64cd\u4f5c\u5f15\u7528\u7b49\u540c\u4e8e\u64cd\u4f5c\u539f\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>\u4f18\u52bf<\/strong>\uff1a\u5b89\u5168\u3001\u9ad8\u6548\u3001\u76f4\u89c2\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u7406\u89e3\u4e86\u5f15\u7528\u7684\u201c\u53d7\u9650\u5236\u6307\u9488\u201d\u672c\u8d28\uff0c\u4f60\u5c31\u80fd\u4ece\u5bb9\u5e94\u5bf9\u53c2\u6570\u4f20\u9012\u3001\u8fd4\u56de\u503c\u8bbe\u8ba1\u3001const \u6b63\u786e\u6027\u7b49\u95ee\u9898\uff0c\u5199\u51fa\u66f4\u4f18\u96c5\u7684 C++ \u4ee3\u7801\u3002\u5e0c\u671b\u8fd9\u7bc7\u6df1\u5165\u6d45\u51fa\u7684\u8bb2\u89e3\u80fd\u5e2e\u52a9\u4f60\u548c\u4f60\u7684\u5b66\u4e60\u8005\u5f7b\u5e95\u638c\u63e1\u8fd9\u4e00\u6838\u5fc3\u7279\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C++ \u91cc\uff0c\u4ec0\u4e48\u65f6\u5019\u52a0\u5f15\u7528\u3001\u4ec0\u4e48\u65f6\u5019\u4e0d\u52a0\u5f15\u7528\uff0c\u6838\u5fc3\u53d6\u51b3\u4e8e\u4e09\u4e2a\u56e0\u7d20\uff1a\u751f\u547d\u5468\u671f\u3001\u4fee\u6539\u9700\u6c42\u548c\u62f7\u8d1d\u6210\u672c\u3002\u4e0b\u9762\u4ece\u8fd4\u56de [&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-1523","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\/1523","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=1523"}],"version-history":[{"count":1,"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/posts\/1523\/revisions"}],"predecessor-version":[{"id":1524,"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/posts\/1523\/revisions\/1524"}],"wp:attachment":[{"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/media?parent=1523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/categories?post=1523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cqfdreamgarden.art\/index.php\/wp-json\/wp\/v2\/tags?post=1523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}