{"id":225,"date":"2019-01-25T23:35:25","date_gmt":"2019-01-26T07:35:25","guid":{"rendered":"http:\/\/35.243.195.209\/?p=225"},"modified":"2019-01-25T23:39:48","modified_gmt":"2019-01-26T07:39:48","slug":"const-pointers-and-references","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/01\/25\/const-pointers-and-references\/","title":{"rendered":"Const, Pointers and References"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce <code>const<\/code>, <code>*<\/code> and <code>&amp;<\/code> and their combinations. I also have similar posts discussed <a href=\"http:\/\/35.243.195.209\/index.php\/2018\/12\/25\/auto-type\/\"><code>auto<\/code> and <code>&amp;<\/code><\/a>.<\/p>\n<h2>Conclusion<\/h2>\n<p>As usual, I present the golden rule first: <strong>&#8220;read from right to left&#8221;<\/strong>. In other words, the <strong>const-on-the-right style<\/strong> always puts the const on the right of what it constifies&#8221;.<\/p>\n<ol>\n<li>When you want a pointer which can not change the object, use <code>CNN const * cnn_ptr = new CNN(model);<\/code>;<\/li>\n<li>When you want a pointer which can not point to other objects, use <code>CNN * const cnn_ptr = new CNN(model);<\/code>;<\/li>\n<li>When you want a pointer which can not change the object and can not point to other objects, use <code>CNN const * const cnn_ptr = new CNN(model);<\/code><\/li>\n<li>When you want a reference which can not change the object, use <code>CNN const &amp; cnn_ref = model;<\/code>;<\/li>\n<\/ol>\n<h2>Details<\/h2>\n<h3>1. <code>const CNN *<\/code> or <code>CNN const *<\/code><\/h3>\n<p>Read it from right to left. <code>CNN const * cnn_ptr<\/code> means \u201ccnn_ptr points to an CNN instance that is const\u201d: the CNN object can\u2019t be changed via <code>cnn_ptr<\/code>. <code>CNN const * cnn_ptr<\/code> is equivalent to <code>CNN const * cnn_ptr<\/code>. However, I prefer the former, which is the <strong>const-on-the-right<\/strong> style.<\/p>\n<p>The const-on-the-right is a consistent style: it always puts the const on the right of what it constifies. In which, a local variable that is const is defined with the const on the right: <code>int const a = 42;<\/code>. Similarly a static variable that is const is defined as <code>static double const x = 3.14;<\/code>. Basically every const ends up on the right of the thing it constifies.<\/p>\n<h3>2. <code>CNN * const cnn_ptr<\/code><\/h3>\n<p>Read it from right to left. <code>CNN * const cnn_ptr<\/code> means \u201ccnn_ptr is a const pointer to an CNN instance that is non-const\u201d: you can\u2019t change the pointer cnn_ptr itself, but you can change the CNN object via the cnn_ptr. <code>const<\/code> constifies <code>*<\/code>, which makes us happy.<\/p>\n<h3>3. <code>CNN const * const cnn_ptr<\/code><\/h3>\n<p>Read it from right to left. It means \u201ccnn_ptr is a const pointer to an CNN instance that is const\u201d: you can\u2019t change the pointer cnn_ptr itself, nor can you change the CNN instance via cnn_ptr.<\/p>\n<h3>4. <code>CNN const &amp; cnn_ref<\/code><\/h3>\n<p>Read it from right to left. \u201ccnn_ref is a reference to a const CNN\u201d. <code>CNN const &amp; cnn_ref<\/code> is equivalent to <code>const CNN &amp; cnn_ref<\/code>.<\/p>\n<h3>5. <code>CNN &amp; const cnn_ref<\/code> ???<\/h3>\n<p>\u201ccnn_ref is a const reference to a CNN\u201d? It makes no sense. Once combined, cnn_ref can not combine other instances. That is, <code>CNN&amp; const cnn_ref<\/code> is always equivalent to <code>CNN&amp; cnn_ref<\/code>.<\/p>\n<h2>Reference<\/h2>\n<p><a href=\"https:\/\/isocpp.org\/wiki\/faq\/const-correctness#const-ptr-vs-ptr-const\">isocpp.org<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce const, * and &amp; and their combinations. I also have similar posts discussed auto and &amp;. Conclusion As usual, I present the golden rule first: &#8220;read from right to left&#8221;. In other words, the const-on-the-right style always puts the const on the right of what it constifies&#8221;. When&#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-225","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\/225","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=225"}],"version-history":[{"count":4,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":232,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/225\/revisions\/232"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}