const qualifier

This article sorts out the use of const in C++ Primer.

1. Once a const object is created, its value cannot be changed, so a const object must be initialized.

 

1  const  int i = get_size();         // correct 
2  const  int j = 42 ;                 // correct 
3  const  int k;                      // error, k is not initialized

 

2. It doesn't matter whether the value used to initialize const is of type const or not.

1  int i = 42 ;              
 2  const  int ci = i; // initialize const with non-const, correct
 3  int j = ci; // const assigns value to non-const, correct

 

3. By default, const objects are only valid within the file

const means that the compiler replaces the place where the variable is used with the corresponding value during the compilation process. In order to implement this substitution, the compiler must know the initial value of the const variable.

If the program contains multiple files, each const object must be able to access its initial value. In order to do this, it must be defined in every file that uses the variable.

In order to support this usage and avoid repeated definitions of the same variable, const objects are only valid in files by default. When const objects with the same name appear in multiple files, independent variables are defined in different files.

 

If sharing within a file is required, use the extern keyword.

1 //file.cpp
2 extern const int i = 3;
3 
4 //file.h
5 extern const int i;

The i of const is defined and initialized in the cpp file, and the knowledge in the header file declares it, indicating that i is not unique to this file, and its definition will appear elsewhere.

 

4.Const reference (constant reference)

(1) Constant references can be bound to constants or non-constants.

1  int i = 10 ;
 2  const  int j = 20 ;
 3  const  int &ii = i;               // a constant reference can be bound to an ordinary variable 
4  const  int &jj = j;              // a constant reference can be bound to a constant object

When binding an ordinary variable, it is not allowed to modify the value of the variable through constant reference, but the value of the variable itself can be modified through assignment, non-constant reference, etc. Such as:

1  int i = 42 ;
 2  int &j = i;                                   // i can be modified by modifying j 
3  const  int &k = i;                            // i cannot be modified by modifying k 
4 i = 24 ;                                    // i can be modified by direct assignment

 

 

(2) A non-const reference cannot bind a constant object

1  const  int i= 10 ;
 2  int &ii = i;                 // Error, i is const and cannot be modified, while ii is a non-const reference, and it is not allowed to modify i through ii

 

5. Pointers and const

Top-level const: Indicates that the pointer itself is a constant.

The underlying const: indicates that the object pointed to by the pointer is a constant.

E.g:

1  int i = 0 ;
 2  int * const p = &i;             // The top-level const, the pointer itself is a constant 
3  const  int *p = &i;             // The underlying const, the variable pointed to by the pointer is a constant

The declaration refers to the underlying const

1  const  int &j = i;             // underlying const

Note: When performing a copy operation, the top-level const can be ignored, but the bottom-level const cannot be ignored! That is to say, if one of the two sides of the copy is modified by the underlying const, then the other must also be modified with the underlying const.

E.g:

1  const  int *p = &i;             // underlying const 
2  int *pp = p;                    // wrong behavior, pp has no underlying const

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342413&siteId=291194637