{"id":207,"date":"2019-01-15T23:16:23","date_gmt":"2019-01-16T07:16:23","guid":{"rendered":"http:\/\/35.243.195.209\/?p=207"},"modified":"2019-01-15T23:17:06","modified_gmt":"2019-01-16T07:17:06","slug":"references-versus-pointers","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/01\/15\/references-versus-pointers\/","title":{"rendered":"References Versus Pointers"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will discuss when to use References and when to use Pointers in C++.<\/p>\n<h2>Conclusion<\/h2>\n<p>As usual, I present the conclusion first.<br \/>\n1. If the varible can be <code>nullptr<\/code>, declare the varible as a pointer;<br \/>\n2. If the you want to re-assign the varible, declare the varible as a pointer;<br \/>\n3. Otherwise, use references.<\/p>\n<h2>Details<\/h2>\n<p>I come across this question when learning the compiler course. Pointers make things complicated when designing compilers in C. And in C++ it becomes more complex after references are introduced.<\/p>\n<h3>Why C++ use pointers?<\/h3>\n<ol>\n<li>For compatibility issue, we know pointers are inherited from C. <\/li>\n<li>In some situations (see the above conclusion), pointers are irreplaceable.<\/li>\n<\/ol>\n<h3>Why introduce references?<\/h3>\n<p>Direct reason that references were introduced in C++ is to support operator overloading.<\/p>\n<pre><code class=\"c\">void f1(const complex* x, const complex* y) \/\/ without references {\n        complex z = *x+*y;  \/\/ ugly\n        \/\/ ...\n}\nvoid f2(const complex&amp; x, const complex&amp; y) \/\/ with references {\n        complex z = x+y;    \/\/ better\n        \/\/ ...\n}   \n<\/code><\/pre>\n<h3>Can referent be a <code>nullptr<\/code>?<\/h3>\n<p><strong>No<\/strong>. Although some compilers will not report it. It\u2019s still illegal. &#8220;The C++ language, as defined by the C++ standard, says it\u2019s illegal.&#8221;<\/p>\n<pre><code class=\"c\">Foo &amp;foo_ref = *foo_pointer;\ncout &lt;&lt; foo_ref.bar &lt;&lt; endl; \/\/ Process finished with exit code 11\n<\/code><\/pre>\n<h3>How to reset a reference variable to another referent?<\/h3>\n<p><strong>No way<\/strong>. Once a reference is bound to an referent, it can not be \u201creseated\u201d to another referent. The reference isn\u2019t a separate object and <strong>the reference is its referent<\/strong>. In that sense, a reference is similar to a const pointer such as int* const p (as opposed to a pointer to const such as const int* p).<\/p>\n<p>See the following example to fully understand it.<\/p>\n<pre><code class=\"c\">class Foo {\npublic:\n    int bar;\n    explicit Foo(int x){\n        bar = x;\n    };\n};\nint main() {\n    Foo foo1(1);\n    Foo foo2(2);\n    Foo &amp;foo_ref = foo1;\n    cout &lt;&lt; foo1.bar &lt;&lt; endl;     \/\/1\n    cout &lt;&lt; foo2.bar &lt;&lt; endl;     \/\/2\n    cout &lt;&lt; foo_ref.bar &lt;&lt; endl;  \/\/1\n    foo_ref = foo2;               \/\/Actually foo1 is a new copy of foo2 now\n    cout &lt;&lt; foo1.bar &lt;&lt; endl;     \/\/2\n    cout &lt;&lt; foo2.bar &lt;&lt; endl;     \/\/2\n    cout &lt;&lt; foo_ref.bar &lt;&lt; endl;  \/\/2\n}\n\n<\/code><\/pre>\n<h2>Reference<\/h2>\n<p><a href=\"https:\/\/isocpp.org\/wiki\/faq\/references#reseating-refs\">ioscpp.org<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will discuss when to use References and when to use Pointers in C++. Conclusion As usual, I present the conclusion first. 1. If the varible can be nullptr, declare the varible as a pointer; 2. If the you want to re-assign the varible, declare the varible as a pointer; 3&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-207","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":1,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":208,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/207\/revisions\/208"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}