{"id":19,"date":"2019-06-16T21:28:43","date_gmt":"2019-06-16T13:28:43","guid":{"rendered":"http:\/\/151.236.221.165\/?p=19"},"modified":"2019-06-18T14:34:36","modified_gmt":"2019-06-18T06:34:36","slug":"why-does-c-have-both-pointers-and-references","status":"publish","type":"post","link":"https:\/\/www.sanburs.xyz\/index.php\/2019\/06\/16\/why-does-c-have-both-pointers-and-references\/","title":{"rendered":"Why does C++ have both pointers and references?"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><br \/>\n\u4e4b\u524d\u4e5f\u4e00\u76f4\u7eb3\u95f7\u8fd9\u4e2a\u95ee\u9898\uff0c\u4e3a\u4ec0\u4e48\u6709\u4e24\u4e2a\u5dee\u4e0d\u591a\u7684\u4e1c\u897f\uff0c\u4ee5\u53ca\u51fd\u6570\u4f20\u53c2\u6570\u7684\u65f6\u5019\u4e3a\u4ec0\u4e48\u4e00\u822c\u90fd\u4f20\u5f15\u7528\uff0c\u5f88\u5c11\u6709\u7528\u6307\u9488\u7684\u3002\u4e4b\u524d\u662f\u6309\u7167\u8fd9\u7bc7\u7406\u89e3\u7684----<a href=\"http:\/\/blog.csdn.net\/u014114990\/article\/details\/51007136\" target=\"_blank\"  rel=\"nofollow\" >\u51fd\u6570\u4f55\u65f6\u503c\u4f20\u9012\uff0c\u4f55\u65f6\u6307\u9488\uff0c\u4f55\u65f6\u5f15\u7528\u4f20\u9012\u603b\u7ed3<\/a><\/p>\n<p><!--more--><\/p>\n<p>\u6587\u7ae0\u5199\u7684\u6709\u70b9\u957f\uff0c\u6458\u70b9\u91cd\u8981\u7684<\/p>\n<ol>\n<li>\u6307\u9488\u4f20\u9012\u9700\u8981\u5f00\u5185\u5b58\uff0c\u5982\u679c\u5fd8\u8bb0\u91ca\u653e\u7684\u8bdd\uff0c\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u9732\u3002\u6d6a\u8d39\u5927\u91cf\u5185\u5b58\u3002\u6240\u4ee5\u5f15\u7528\u4f20\u9012\u66f4\u597d\u4e00\u4e9b\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u8fd8\u53ef\u4ee5\u8ba9\u5b83\u4ee5\u201c\u5f15\u7528\u4f20\u9012\u201d\u65b9\u5f0f\u800c\u4e0d\u662f\u4ee5\u201c\u503c\u4f20\u9012\u201d\u65b9\u5f0f\u8fd4\u56de\uff1a<code>int &amp;myFuntion();<\/code><\/p>\n<\/li>\n<li>\n<p>\u9664\u4e86\u53ef\u4ee5\u6539\u53d8\u6709\u5173\u53d8\u91cf\u7684\u503c\uff0c\u201c\u5f15\u7528\u4f20\u9012\u201d\u65b9\u5f0f\u7684\u53e6\u4e00\u4e2a\u597d\u5904\u662f\u5b83\u7684\u5f00\u9500\u76f8\u5bf9\u8981\u5c0f\u4e00\u4e9b\uff1a\u56e0\u4e3a\u4e0d\u9700\u8981\u5728\u51fd\u6570\u91cc\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\u6765\u5bb9\u7eb3\u90a3\u4e9b\u503c\uff0c\u7a0b\u5e8f\u7684\u5185\u5b58\u5360\u7528\u91cf\u5f53\u7136\u4f1a\u5c0f\u4e00\u4e9b\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u60f3\u83b7\u5f97\u201c\u5f15\u7528\u4f20\u9012\u201d\u65b9\u5f0f\u5e26\u6765\u7684\u6027\u80fd\u6539\u5584\uff0c\u4f46\u4e0d\u60f3\u6539\u53d8\u67d0\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u628a\u76f8\u5e94\u7684\u8f93\u5165\u53c2\u6570\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5e38\u91cf\uff1a<code>void myFunc(const int &amp;myNum);<\/code>\u901a\u8fc7\u8fd9\u6837\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u628a\u5177\u4f53\u7684\u53c2\u6570\u76f4\u63a5\u4f20\u9012\u7ed9\u5b83\uff1a<code>myFunc(7);<\/code><\/p>\n<\/li>\n<li>\n<p>\u6700\u91cd\u8981\u7684\u4e00\u70b9\uff0c\u4e5f\u662f\u4e0a\u6587\u4e2d\u82b1\u7b14\u58a8\u6700\u591a\u7684\u3002\u7b80\u8ff0\u4e00\u4e0b\uff1a\u6307\u9488\u4f20\u9012\u7684\u672c\u8d28\u4e5f\u662f\u8ddf\u503c\u4f20\u9012\u4e00\u6837\uff0c\u53ea\u4e0d\u8fc7\u4e0d\u662f\u503c\u7684\u62f7\u8d1d\u800c\u662f\u5730\u5740\u7684\u62f7\u8d1d\u3002\u8fd9\u4f1a\u5f15\u53d1\u4e00\u4e2a\u95ee\u9898\uff0c\u5982<\/p>\n<pre><code class=\"language-cpp \"> char *str = \"this is a test\";\n   int change(char* name)\n   {\n       name=\"alter\";\n       return 1;\n   }\n    change(str);\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4f20\u9012\u53c2\u6570\u7684\u65f6\u5019\uff0cstr\u548cname\u867d\u7136\u90fd\u662f\u5b58\u7684\u201cthis is a test\u201d\u5b57\u7b26\u4e32\u7684\u9996\u5730\u5740\uff0c\u4f46&amp;str\u548c&amp;name\u662f\u4e0d\u4e00\u6837\u7684,\u662f\u4e24\u4e2a\u5730\u5740\u3002\u8fd9\u6837\u518d\u5bf9name=\"alter\"\u7684\u65f6\u5019\uff0cname\u5b58\u7684\u5730\u5740\u53d8\u5316\u4e86\uff0c\u4f46\u662fstr\u6ca1\u53d8\u3002\u8fd9\u91cc\u5e94\u8be5\u4f20\u4e8c\u7ea7\u6307\u9488\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u4f7f\u7528\u5f15\u7528\u7684\u8bdd\u5c31\u6ca1\u4e8b\u3002\u662f\u4e00\u6837\u7684\u5730\u5740\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h2>C++\u539f\u4f5c\u8005\u56de\u7b54<\/h2>\n<h3>Why does C++ have both pointers and references?<\/h3>\n<p><a href=\"http:\/\/www.stroustrup.com\/bs_faq2.html#pointers-and-references\" target=\"_blank\"  rel=\"nofollow\" >[Why does C++ have both pointers and references?]<\/a><\/p>\n<p>C++ inherited pointers from C, so I couldn't remove them without causing serious compatibility problems. References are useful for several things, but the direct reason I introduced them in C++ was to support operator overloading. For example:<\/p>\n<pre><code class=\"language-cpp \">    void f1(const complex* x, const complex* y) \/\/ without references\n    {\n        complex z = *x+*y;  \/\/ ugly\n        \/\/ ...\n    }\n\n    void f2(const complex&amp; x, const complex&amp; y) \/\/ with references\n    {\n        complex z = x+y;    \/\/ better\n        \/\/ ...\n    }   \n<\/code><\/pre>\n<p>More generally, if you want to have both the functionality of pointers and the functionality of references, you need either two different types (as in C++) or two different sets of operations on a single type. For example, with a single type you need both an operation to assign to the object referred to and an operation to assign to the reference\/pointer. This can be done using separate operators (as in Simula). For example:<\/p>\n<pre><code class=\"language-cpp \">    Ref&lt;My_type&gt; r :- new My_type;\n    r := 7;         \/\/ assign to object\n    r :- new My_type;   \/\/ assign to reference\n<\/code><\/pre>\n<p>Alternatively, you could rely on type checking (overloading).For example:<\/p>\n<pre><code class=\"language-cpp \">    Ref&lt;My_type&gt; r = new My_type;\n    r = 7;          \/\/ assign to object\n    r = new My_type;    \/\/ assign to reference\n<\/code><\/pre>\n<hr \/>\n<h3>Should I use call-by-value or call-by-reference?<\/h3>\n<p>That depends on what you are trying to achieve:<\/p>\n<ul>\n<li>If you want to change the object passed, call by reference or use a pointer; e.g. void f(X&amp;); or void f(X*);<\/li>\n<li>If you don't want to change the object passed and it is big, call by const reference; e.g. void f(const X&amp;);<\/li>\n<li>Otherwise, call by value; e.g. void f(X);<\/li>\n<\/ul>\n<p>What do I mean by \"big\"?<\/p>\n<p>Anything larger than a couple of words.(\u56e0\u4e3a\u6307\u9488\u4f20\u9012\u9700\u8981\u5f00\u5185\u5b58)<\/p>\n<p><em>Why would I want to change an argument?<\/em> Well, often we have to, but often we have an alternative: produce a new value. Consider:<\/p>\n<pre><code class=\"language-cpp \">    void incr1(int&amp; x); \/\/ increment\n    int incr2(int x);   \/\/ increment\n\n    int v = 2;\n    incr1(v);   \/\/ v becomes 3\n    v = incr2(v);   \/\/ v becomes 4\n<\/code><\/pre>\n<p>I think that for a reader, incr2() is easier to understand. That is, incr1() is more likely to lead to mistakes and errors. So, I'd prefer the style that returns a new value over the one that modifies a value as long as the creation and copy of a new value isn't expensive.<\/p>\n<p><em>I do want to change the argument, should I use a pointer or should I use a reference?<\/em> I don't know a strong logical reason. If passing ``not an object'' (e.g. a null pointer) is acceptable, using a pointer makes sense. My personal style is to use a pointer when I want to modify an object because in some contexts that makes it easier to spot that a modification is possible.<\/p>\n<p>Note also that a call of a member function is essentially a call-by-reference on the object, so we often use member functions when we want to modify the value\/state of an object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u4e5f\u4e00\u76f4\u7eb3\u95f7\u8fd9\u4e2a\u95ee\u9898\uff0c\u4e3a\u4ec0\u4e48\u6709\u4e24\u4e2a\u5dee\u4e0d\u591a\u7684\u4e1c\u897f\uff0c\u4ee5\u53ca\u51fd\u6570\u4f20\u53c2\u6570\u7684\u65f6\u5019\u4e3a\u4ec0\u4e48\u4e00\u822c\u90fd\u4f20\u5f15\u7528\uff0c\u5f88\u5c11\u6709\u7528\u6307\u9488\u7684\u3002\u4e4b\u524d\u662f\u6309\u7167\u8fd9\u7bc7\u7406\u89e3\u7684&#8211; &#8230","protected":false},"author":1,"featured_media":32,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/19"}],"collection":[{"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":6,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":50,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/19\/revisions\/50"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/media\/32"}],"wp:attachment":[{"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}