{"id":186,"date":"2009-09-21T17:21:06","date_gmt":"2009-09-21T09:21:06","guid":{"rendered":"http:\/\/www.genepeng.com\/index.php\/186"},"modified":"2009-09-21T17:21:06","modified_gmt":"2009-09-21T09:21:06","slug":"const-objects-are-local-to-a-file-by-default","status":"publish","type":"post","link":"https:\/\/www.genepeng.com\/index.php\/186","title":{"rendered":"const Objects Are Local to a File By Default"},"content":{"rendered":"<p>When we define a non<tt>const<\/tt> variable at global scope, it is accessible throughout the program. We can define a non<tt>const<\/tt> variable in one file andassuming an appropriate declaration has been madecan use that variable in another file:<\/p>\n<pre>      \/\/ file_1.cc\n      int counter;  \/\/ definition\n      \/\/ file_2.cc\n      extern int counter; \/\/ uses counter from file_1\n      ++counter;          \/\/ increments counter defined in file_1<\/pre>\n<p>Unlike other variables, unless otherwise specified, <tt>const<\/tt> variables declared at global scope are local to the file in which the object is defined. The variable exists in that file only and cannot be accessed by other files.<\/p>\n<p>We can make a <tt>const<\/tt> object accessible throughout the program by specifying that it is <tt>extern<\/tt>:<\/p>\n<pre>      \/\/ file_1.cc\n      \/\/ defines and initializes a const that is accessible to other files\n      extern const int bufSize = fcn();\n      \/\/ file_2.cc\n      extern const int bufSize; \/\/ uses bufSize from file_1\n      \/\/ uses bufSize defined in file_1\n      for (int index = 0; index != bufSize; ++index)\n            \/\/ ...<\/pre>\n<p><a name=\"idd1e11570\"><\/a><a name=\"idd1e11573\"><\/a><a name=\"idd1e11576\"><\/a>In this program, <tt>file_1.cc<\/tt> defines and initializes <tt>bufSize<\/tt> to the result returned from calling the function named <tt>fcn<\/tt>. The definition of <tt>bufSize<\/tt> is <tt>extern<\/tt>, meaning that <tt>bufSize<\/tt> can be used in other files. The declaration in <tt>file_2.cc<\/tt> is also made <tt>extern<\/tt>. In this case, the <tt>extern<\/tt> signifies that <tt>bufSize<\/tt> is a declaration and hence no initializer is provided.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we define a nonconst variable at global scope, it is accessible throughout the program. We can define a nonconst variable in one file andassuming an appropriate declaration has been madecan use that variable in another file: \/\/ file_1.cc int counter; \/\/ definition \/\/ file_2.cc extern int counter; \/\/ uses counter from file_1 ++counter; \/\/ &#8230; <a title=\"const Objects Are Local to a File By Default\" class=\"read-more\" href=\"https:\/\/www.genepeng.com\/index.php\/186\" aria-label=\"More on const Objects Are Local to a File By Default\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/186"}],"collection":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":0,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}