(1) The non-const variable of the const variable defaults to extern. To make the const variable accessible in other source files, you must specify it as extern. Otherwise, an error occurs: error LNK2001: the external symbol "int const BUF_SIZE "(? BUF_SIZE @ 3HB)
(2) The const qualifier can be placed before or after the type. The following s1 and s2 types are the same: string const s1; const string s2; (3) identification of pointer and const: draw a vertical line on the left or right to see if it is with const. It cannot be changed if it is with const. Const void * p1 cannot be changed, but the content pointed to by p1 cannot be changed. If const appears on the left side of "*", it modifies the object to which the Pointer Points. The indicated object is a constant, that is, the object cannot be modified. If const appears on the right side of "*", it modifies the pointer itself. The pointer itself is a constant, that is, it cannot point to other memory. If the const appears on both sides of the * sign, the pointer and the indicated object are constants. (3.1) the pointer to the const object. To ensure that the Pointer Points to the const object, you are not allowed to use the pointer to change the const value, the C ++ language requires that the pointer to the const object must also have the const feature. Assigning the address of a const object to a normal, non-const object pointer will also lead to a compilation error: const double pi = 3.14; double * ptr = & pi; // error: ptr is a plain pointer const double * cptr = & pi; // OK: cptr is a pointer to const. You cannot use the void * pointer to save the address of the const object, you must use a const void * pointer to save the const object address: const int universe = 42; const void * cpv = & universe; // OK: cpv is const void * pv = & universe; // error: universe is const allows the address of a non-const object to be assigned to a pointer to a const object. For example: For example, double dval = 3.14; // dval is adouble; its value can be changed const double * cptr = & dval; // OK: but can't change dval through cptr although the specified object dval is not const, you still cannot use cptr to modify the value of this object, any attempt to modify its value through the pointer cptr will result in a compilation error. Essentially, since there is no way to determine whether the object referred to by cptr is const, the system regards all objects referred to by cptr as const. You cannot use a pointer to a const object to modify the base object. You can use other methods to modify the value of a non-const object to which the const Pointer Points. The value of the object pointed to by the pointer to const cannot be modified. The pointer to const can be understood as "self-thinking pointer to const ". In actual programs, pointers to const are often used as function parameters. Define the form parameter as a pointer to the const to ensure that the actual object passed to the function is not modified because of the form parameter. (3.2) the value of the const pointer cannot be changed to int errNumb = 0; int * const curErr = & errNumb; // curErr is a constant pointer we can read the above definition statement from the right to the left as "curErr is the const pointer to the int type object ". Like other const values, the value of the const pointer cannot be modified, which means that curErr cannot be directed to other objects. Any attempt to assign values to the const pointer will result in compilation errors. Like any const volume, the const pointer must also be initialized during definition. The fact that the pointer itself is a const does not indicate whether the pointer can be used to modify the value of the object it points. Whether the value of the object indicated by the pointer can be modified depends on the object type. (3.3) the const pointer to the const object const double pi = 3.14159; const double * const pi_ptr = & pi; in this example, the value of the object pointed to by pi_ptr cannot be modified, it is not allowed to modify the pointer point (that is, the address value stored in pi_ptr ). You can read the statement from the right to the left: "pi_ptr is a const pointer, pointing to a double-type const object ". (4) Application of const in functions (4.1) const-type parameters that are not referenced are equivalent to const-type parameters. Whether to define the form parameter as const. This difference does not affect the object passed to the function. The reason is that the method of passing real parameters. When copying a parameter, you do not need to consider whether the parameter is const. function operations only involve copies. The function cannot modify the real parameter. You can pass a const object to a const parameter or a non-const parameter. There is no essential difference between the two parameters. Functions with const reference parameters are different from those with non-const reference parameters. Similarly, if a function has a pointer to the const type, it is different from a function with a pointer to a non-const object of the same type. Only when the parameter is a reference or pointer, whether the parameter is a const is affected. (4.2) using const references to avoid copying. Using referenced parameters, the function can directly access the real parameter object without copying it. (4.3) when the parameter is not referenced, the function overload cannot be implemented based on whether the parameter is const. F (int *); f (int * const); // redeclaration at this time, const is used to modify the pointer itself, instead of modifying the type pointed to by the pointer. In both cases, the pointer is copied, and whether the pointer itself is const makes no difference. When a parameter is transmitted as a copy, it cannot be overloaded based on whether the parameter is a const.