[C ++ learning notes] const qualifier, learning notes const

Source: Internet
Author: User

[C ++ learning notes] const qualifier, learning notes const

The main reference for learning the const qualifier is the typical textbook C ++ primer. This document is purely a learning note, and the quotation marks in the sentence below are the original words in the book.

I am not very strong at C ++. If you have any misunderstandings or comments in this article, please point out.

 

1. const qualifier

"Sometimes we want to define a variable whose value cannot be changed. "In C ++, to meet this requirement, you can use the const qualifier to limit the type of the variable. The const qualifier is one way to define constants in c ++, and another way is to use # define to define macro constants.

2. const object initialization

Because the value of a const object cannot be changed once it is created, the const object must be initialized during definition. The book provides two methods of const object initialization:

Const int I = get_size (); // initialization at runtime

Cosnt int j = 42; // initialization at compilation

Const int k; // error: k is an uninitialized constant.

Constant I is initialized when the program is running. The program obtains the return value of the get_size () function and initializes the constant I. The constant j is directly initialized to the nominal value constant "42" during program compilation ". Because constant k does not assign an initial value, the compiler reports the error "uninitialized const 'K '".

"A const object can perform most of the operations that non-const objects can participate in. The main restriction is that it can only perform operations on a const object without changing its content, one operation is to initialize other variables. That is to say, we can use a const type object to initialize a const type or non-const type object, as shown in the following code, three 10 records can be compiled and executed successfully.

 1 int main(int argc, char *argv[]) 2 { 3     const int a = 10; 4     int b = a; 5     const int c = a; 6     cout<< a <<endl; 7     cout<< b <<endl; 8     cout<< c <<endl; 9     return 0;10 }

3. the const object is only valid in the file by default.

"To support this practice (that is, every file that uses this constant has the definition of this constant) and avoid repeated definitions of the same variable. By default, the const object is set to only valid in the file. "

I do not particularly understand this sentence. Why should C ++ make the const object only valid in the file? I have made a mark for the moment, and I have not been able to find the relevant information.

The following only describes the valid nature of the const object file.

According to my understanding, what is effective in the file is global, and general variables defined outside the function are global, for example, if both file1.cpp and file2.cpp have definitions of int type variable a, the compiler reports an error "error: multiple definition of 'A '". That is to say, for a globally valid variable, the definition of the same variable name cannot exist in different files of a project. To solve this problem, we can change the variable name or use the namespace.

//file1.cppint a = 10;
1 //file2.cpp2 int a = 5;

The const object does not have this restriction. we can define the const object with the same name in file1.cpp and file2.cpp without the problem of redefinition. As shown below.

1 //file1.cpp2 const int a = 10;
1 file2.cpp2 const int a = 5;

For non-const variable a above, we only wanted to use the variable a defined in file1.cpp in file2.cpp, then, we only need to change the definition of variable a in file2.cpp to declarative, and change int a = 5 to extern int a, so that we can continue to use variable a in file2.cpp.

The const object is not the same, because the const object is only valid in the file, so if you need to change the const object to global, and use it in different files, we need to add the extern modifier to the definition and description of the const object (the above non-const type variable a only needs to add the extern modifier before the declaration), that is

1 //file1.cpp2 extern const int a = 10;
1 //file2.cpp2 extern const int a;

According to my understanding, adding the extern modifier to the definition of the const object seems to mean that the "const object is valid in the file by default" attribute is changed to global effect, the const object is changed to be the same as a common variable.

 

Related Article

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.