{"id":3949,"date":"2013-12-05T15:15:36","date_gmt":"2013-12-05T13:15:36","guid":{"rendered":"http:\/\/www.cloudcomp.ch\/?p=3949"},"modified":"2014-03-28T15:22:08","modified_gmt":"2014-03-28T13:22:08","slug":"const-in-cpp-a-brief-overview","status":"publish","type":"post","link":"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/","title":{"rendered":"Const in C++, a brief overview"},"content":{"rendered":"<p>A word of warning at the beginning: This post is about C++ and not about C! So whatever you read here may not necessarily apply to C. Nevertheless you may have a look <a href=\"http:\/\/web.eecs.umich.edu\/~sugih\/pointers\/C++NotInC.html\">Sugih&#8217;s page which features are in C<\/a>. Besides I&#8217;m not going to explain what references and pointers are.<\/p>\n<p><h1>Introduction<\/h1>\n<p>While working on the <a title=\"FI-PPP FI-WARE (KIARA)\" href=\"http:\/\/www.cloudcomp.ch\/research\/foundation\/projects\/kiara\/\">KIARA project<\/a> writing C++11 code I was faced with the task of passing variables to functions which will not alter these. Or pure getter methods. And after reading a post about <a href=\"http:\/\/stackoverflow.com\/questions\/5598703\/c-const-usage-explanation\">C++ constness<\/a> I was left with more questions than really understanding. A quick search revealed <a href=\"http:\/\/www.parashift.com\/c++-faq-lite\/const-correctness.html\">thorough FAQ about const usage in C++<\/a>. Here I&#8217;d like to make a short round-up of it.<\/p>\n<h1>How to read <code>const<\/code><\/h1>\n<p>First of all, read it from right-to-left. For instance <code>int const* const p<\/code> would be read as &#8220;p is a constant pointer to a constant int&#8221;.<\/p>\n<h1>A few examples<\/h1>\n<h2><code>string const&amp; s<\/code><\/h2>\n<p>Let&#8217;s say you read something like this:<\/p>\n<pre>\nvoid MyClass::func (std::string const&amp; s);\n<\/pre>\n<p>So reading from right-to-left this would say &#8220;s is reference to a constant std::string&#8221;. This implies that <code>func<\/code> is not going to modify <code>s<\/code>. But keep in mind that you may have a dangling reference here, especially if you work with multi-threaded code. If this would be a pointer instead of a reference it is still possible the pointer to be NULL.<\/p>\n<h2><code>string* const s<\/code><\/h2>\n<p><pre>\nvoid MyClass::func (std::string* const s);\n<\/pre>\n<p>Again, read from right-to-left: &#8220;s is a constant pointer to a string&#8221;. <code>s<\/code> may be modified (the object itself) but <code>s<\/code> may not point to a different object. As you may have already noticed it&#8217;s not that hard to read when you stick to the &#8220;read from right-to-left&#8221; rule.<\/p>\n<h2><code>string const* const s<\/code><\/h2>\n<p><pre>\nvoid MyClass::func (std::string const* const s);\n<\/pre>\n<p>This would read &#8220;s is a constant pointer to a constant string&#8221; and therefore the function guarantees (respectively the compiler enforces it) that the content of <code>s<\/code> may not be changed nor may <code>s<\/code> point to a different object.<\/p>\n<h2>Similar consts<\/h2>\n<p>Obviously some signatures mean the same thing:<\/p>\n<pre>\nvoid MyClass::func (const std::string&amp; s); \/\/ you may know this from C\nvoid MyClass::func (std::string const&amp; s);\n\nvoid MyClass::func (const std::string* s);\nvoid MyClass::func (std::string const* s); \/\/ equivalent\nvoid MyClass::func (std::string* const s); \/\/ Pitfall: NOT equivalent\n<\/pre>\n<p>Now, it doesn&#8217;t matter which version you use but is more a decision you have to make. If previously written code opts for one variant you should for the sake of consistency use the same way.<\/p>\n<h2>Methods that do not change its object<\/h2>\n<p>When you write a pure getter method you may want to tell your compiler that the following code may not change the object itself.<\/p>\n<pre>\nstd::string const&amp; MyClass::func () const;\n<\/pre>\n<p>The last const indicates that this function may not alter its objects data.<\/p>\n<h1>Conclusion<\/h1>\n<p>C++ is not a language you master in a few hours of training, C++ is like meditating and takes daily training.<\/p>\n<div class=\"pt-sm\">Schlagw\u00f6rter: <a href=\"https:\/\/blog.zhaw.ch\/icclab\/tag\/c\/\">C++<\/a>, <a href=\"https:\/\/blog.zhaw.ch\/icclab\/tag\/const\/\">const<\/a>, <a href=\"https:\/\/blog.zhaw.ch\/icclab\/tag\/pointer\/\">pointer<\/a>, <a href=\"https:\/\/blog.zhaw.ch\/icclab\/tag\/reference\/\">reference<\/a><br><\/div>","protected":false},"excerpt":{"rendered":"<p>A word of warning at the beginning: This post is about C++ and not about C! So whatever you read here may not necessarily apply to C. Nevertheless you may have a look Sugih&#8217;s page which features are in C. Besides I&#8217;m not going to explain what references and pointers are. Introduction While working on [&hellip;]<\/p>\n","protected":false},"author":61,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[15],"tags":[60,96,258,280],"features":[],"class_list":["post-3949","post","type-post","status-publish","format-standard","hentry","category-howtos","tag-c","tag-const","tag-pointer","tag-reference"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Const in C++, a brief overview - Service Engineering (ICCLab &amp; SPLab)<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Const in C++, a brief overview\" \/>\n<meta property=\"og:description\" content=\"A word of warning at the beginning: This post is about C++ and not about C! So whatever you read here may not necessarily apply to C. Nevertheless you may have a look Sugih&#8217;s page which features are in C. Besides I&#8217;m not going to explain what references and pointers are. Introduction While working on [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/\" \/>\n<meta property=\"og:site_name\" content=\"Service Engineering (ICCLab &amp; SPLab)\" \/>\n<meta property=\"article:published_time\" content=\"2013-12-05T13:15:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-03-28T13:22:08+00:00\" \/>\n<meta name=\"author\" content=\"habl\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"habl\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/\"},\"author\":{\"name\":\"habl\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac\"},\"headline\":\"Const in C++, a brief overview\",\"datePublished\":\"2013-12-05T13:15:36+00:00\",\"dateModified\":\"2014-03-28T13:22:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/\"},\"wordCount\":422,\"commentCount\":0,\"keywords\":[\"C++\",\"const\",\"pointer\",\"reference\"],\"articleSection\":[\"HowTos\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/\",\"url\":\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/\",\"name\":\"Const in C++, a brief overview - Service Engineering (ICCLab &amp; SPLab)\",\"isPartOf\":{\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/#website\"},\"datePublished\":\"2013-12-05T13:15:36+00:00\",\"dateModified\":\"2014-03-28T13:22:08+00:00\",\"author\":{\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac\"},\"breadcrumb\":{\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Startseite\",\"item\":\"https:\/\/blog.zhaw.ch\/icclab\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Const in C++, a brief overview\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/#website\",\"url\":\"https:\/\/blog.zhaw.ch\/icclab\/\",\"name\":\"Service Engineering (ICCLab &amp; SPLab)\",\"description\":\"A Blog of the ZHAW Zurich University of Applied Sciences\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.zhaw.ch\/icclab\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac\",\"name\":\"habl\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g\",\"caption\":\"habl\"},\"url\":\"https:\/\/blog.zhaw.ch\/icclab\/author\/habl\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Const in C++, a brief overview - Service Engineering (ICCLab &amp; SPLab)","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/","og_locale":"en_US","og_type":"article","og_title":"Const in C++, a brief overview","og_description":"A word of warning at the beginning: This post is about C++ and not about C! So whatever you read here may not necessarily apply to C. Nevertheless you may have a look Sugih&#8217;s page which features are in C. Besides I&#8217;m not going to explain what references and pointers are. Introduction While working on [&hellip;]","og_url":"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/","og_site_name":"Service Engineering (ICCLab &amp; SPLab)","article_published_time":"2013-12-05T13:15:36+00:00","article_modified_time":"2014-03-28T13:22:08+00:00","author":"habl","twitter_card":"summary_large_image","twitter_misc":{"Written by":"habl","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/#article","isPartOf":{"@id":"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/"},"author":{"name":"habl","@id":"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac"},"headline":"Const in C++, a brief overview","datePublished":"2013-12-05T13:15:36+00:00","dateModified":"2014-03-28T13:22:08+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/"},"wordCount":422,"commentCount":0,"keywords":["C++","const","pointer","reference"],"articleSection":["HowTos"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/","url":"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/","name":"Const in C++, a brief overview - Service Engineering (ICCLab &amp; SPLab)","isPartOf":{"@id":"https:\/\/blog.zhaw.ch\/icclab\/#website"},"datePublished":"2013-12-05T13:15:36+00:00","dateModified":"2014-03-28T13:22:08+00:00","author":{"@id":"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac"},"breadcrumb":{"@id":"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.zhaw.ch\/icclab\/const-in-cpp-a-brief-overview\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Startseite","item":"https:\/\/blog.zhaw.ch\/icclab\/"},{"@type":"ListItem","position":2,"name":"Const in C++, a brief overview"}]},{"@type":"WebSite","@id":"https:\/\/blog.zhaw.ch\/icclab\/#website","url":"https:\/\/blog.zhaw.ch\/icclab\/","name":"Service Engineering (ICCLab &amp; SPLab)","description":"A Blog of the ZHAW Zurich University of Applied Sciences","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.zhaw.ch\/icclab\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac","name":"habl","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g","caption":"habl"},"url":"https:\/\/blog.zhaw.ch\/icclab\/author\/habl\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/posts\/3949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/users\/61"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/comments?post=3949"}],"version-history":[{"count":1,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/posts\/3949\/revisions"}],"predecessor-version":[{"id":4272,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/posts\/3949\/revisions\/4272"}],"wp:attachment":[{"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/media?parent=3949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/categories?post=3949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/tags?post=3949"},{"taxonomy":"features","embeddable":true,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/features?post=3949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}