C++primer:const Qualifier

Source: Internet
Author: User

1. Problem Introduction
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >for (int index =0; Index! =, + + index) {     //......</span></span>
There are two problems with the for loop code above, which are related to 512 as the upper bounds of the loop. The first problem is the readability of a program. What's the point of comparing index and 512??? What kind of work does the loop do??? Which means 512 is what??? In this example, 512 is called the magic number, and its meaning is not reflected in the context, as if the number had magically appeared from the air. 'The second problem is the maintainability of the program. Assuming this program is very large, 512 appears 100 times. Further assume that in these 100 times, 80 times is the size of a particular buffer, and the remaining 20 times are used for other purposes. Now we need to increase the size of the buffer to 1024. To achieve this change, you must check where each 512 appears. At this point we have to carefully investigate which means the buffer zone, which is the other purpose, you know, the wrong change, will cause the collapse of the program!!! The solution to these two problems is to use an object initialized to 512:
<span style= "FONT-SIZE:18PX;" >int bufSize = 512; Input buffer sizefor (int index = 0; Index! = bufSize; ++index) {  //...} </span>
As we can see, it is now tested against the object bufsize, not the literal constant 512.2.Why do we need a const?There is still a serious problem with the method of defining a variable to represent a constant. That is, bufSize can be modified. BufSize may be intentionally or unintentionally modified. The Const qualifier provides a workaround that transforms an object into a constant.
<span style= "FONT-SIZE:18PX;" >const int bufSize = 512; Input buffer size</span>
defines Bufsixe as a constant and initializes to 512. The variable bufsize is still an lvalue. But now this lvalue is not modifiable, and any attempt to modify any of the bufsize will result in a compilation error.
<span style= "FONT-SIZE:18PX;" >bufsize = 0;//error:attemp to write to const object</span>
Because constants cannot be modified after they are defined, they must be initialized at the time of definition!!!
<span style= "FONT-SIZE:18PX;" >const std::string hi = "Hello";//Ok:innitializedconst int i,j=0;//error:i is uninitialized const</span>
3.const object defaults to a local variable of the file When you define a non-const variable in a global scope, it is accessible throughout the program . We can define a non-const change in a file, assuming that the appropriate declaration has been made, you can use the variable in another file:
<span style= "FONT-SIZE:18PX;" >//file_1.ccint counter; defination//File_2.ccextern int counter; Uses counter from File_1++counter; Increments counter defined in file_1</span>
Unlike other variables, a const variable declared at the global scope is a local variable of the file that defines the object, unless otherwise noted . This variable exists only in that file and cannot be accessed by another file .
<span style= "FONT-SIZE:18PX;" >//file_1.cc//defines and initialize a const that's accessible to other filesextern const int bufSize = FCN ();//fil E_2.ccextern const int bufSize; Uses bufSize from file_1for (int index = 0; Index! = bufSize; + + index) {  //...} </span>
In the above program, file_1.cc defines and initializes bufsize through the return value of the function FCN. BufSize is also defined as an external type extern, which means that bufsize can be used in other files. The bufsize in file_2.cc is also declared as an extern type, and cannot be initialized. difference: Non-const variables are automatically default to extern. In order for the const variable to be accessible in other files, he must be braked as extern.



















C++primer:const Qualifier

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.