[C + + learning note] Const qualifier

Source: Internet
Author: User

The main reference to the Const qualifier is the classic textbook "C + + Primer", this article is purely study notes, the following quoted sentences are the words in the book.

I C + + Foundation is not very strong, if there is any misunderstanding and speech, please point out.

1, const qualifier

"Sometimes we want to define a variable whose value cannot be changed. In C + +, to satisfy this requirement, the variable's type can be qualified with the Const qualifier. The const qualifier is one way to define constants in C + + and another way to define macro constants by using # #.

2. Const object Initialization

Because the const object cannot be changed once it has been created, the Const object must be initialized when it is defined. Two types of initialization of const objects are given in the book:

const int i = Get_size (); Run-time initialization

cosnt int j = 42; Compile-time initialization

const int k; Error: K is a non-initialized constant

The constant I is initialized when the program is run, and the program obtains the return value of the get_size () function and initializes the constant I. The constant, J, is directly initialized to the literal value "42" at the time of program compilation. Constant k because there is no initial value assigned, the compiler will error "uninitialized const ' K '".

"A const type object can accomplish most of the operations that a non-const type can participate in, and the main limitation is that it can only perform operations that do not alter its contents on objects of the const type, one of which is to initialize the other variables", that is, We can use the Const type object to initialize a const type or non-const type object, as shown in the following code, to successfully compile the execution output 3 of 10.

1 intMainintargcChar*argv[])2 {3     Const intA =Ten;4     intb =A;5     Const intc =A;6cout<< a <<Endl;7cout<< b <<Endl;8cout<< C <<Endl;9     return 0;Ten}

3. The const object is only valid within the file by default

"To support this approach (that is, every file that uses that constant has a definition for that constant) and avoids duplicate definitions of the same variable, by default, the const object is set to be valid only within the file. "

I do not particularly understand this sentence, why C + + to make the const object only valid in the file? Make a mark for the time being, I also could not find the relevant information.

The following is only a description of the properties of the const object that are valid in the file.

As I understand it, the valid relative to the file is globally valid, the definition of the normal variable outside the function is globally valid, for example, in the following file1.cpp and file2.cpp two files have a definition of the int variable A, then the compiler will error "Error: Multiple definition of ' a '. In other words, for globally valid variables, the same variable name cannot be defined in different files in a project, in order to solve this problem we can change the variable name or use the name space namespace.

// File1.cpp int ten;
1 // File2.cpp 2 int 5;

The const object does not have this limitation, and we can define a const object of the same name in File1.cpp and file2.cpp without having to redefine the problem. as shown below.

1// File1.cpp 2 Const int ten;
1 File2.cpp 2 Const int 5;

If for the above non-const type variable A, our intention is only to use the variable a defined in the file1.cpp in the file2.cpp, then we just need to change the definition of variable a in file2.cpp to the declaration, we will change int a = 5 to extern int A, We can continue to use variable a in file2.cpp.

The const object is not the same, because the const object is valid only within the file, so if you need to make the const object globally valid and use it in a different file, We need to add the extern modifier to the definition and declaration of the Const object (the non-const variable A above simply adds the extern modifier before the declaration), i.e.

1 // File1.cpp 2 extern Const int ten;
1 // File2.cpp 2 extern Const int A;

As I understand it, the definition of a const object, with the extern modifier, seems to change the nature of the "const object to be valid in file" property to be globally valid, just as the const object becomes the same as a normal variable.

[C + + learning note] 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.