The const keyword exists in both C and C ++ languages. Many people have a wrong idea about this keyword. In C, the const keyword is used to make the variable as a constant, variable normalization is like macro definition. In C, the keyword const does not restrict variables rather than constants, so that variables cannot be re-assigned except the initial values.
In C ++, the keyword const not only makes the variable unmodifiable, but also makes the variable constant. After the variable is assigned the initial value, it can be used as a constant, which is equivalent to macro definition.
Enter the following code in the compiler to give you a more intuitive experience.
In the C language compiler:
/* The value of the variable restricted by const cannot be modified */<br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> const int A = 1; <br/> int const B = 1; <br/> A = 1; <br/> B = 1; <br/> system ("pause"); <br/> return 0; <br/>}
When you compile this code, the compiler reports an error: the const restriction object cannot be modified in the mian function.
Note: in C language, the keyword const is put before or after the identifier, which is the same effect.
/* Variables restricted by const cannot be modified */<br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> const int A = 1; <br/> switch (1) <br/>{< br/> case A: prinf ("variable A can be used as constant"); break; <br/> default: printf ("variable A cannot be used as constant"); <br/>}< br/> system ("pause"); <br/> return 0; <br/>}< br/>
When you compile this code, the compiler reports an error: constants are required in the main function.
Restrictions on pointer by the keyword const:
/* Before cosnt *, the pointer to the object value cannot be modified */<br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> int A = 1, B = 2; <br/> const int * P1 = & A; <br/> int const * P2 = & B; <br/> * P1 = 1; <br/> * P2 = 1; <br/> system ("pause"); <br/> return 0; <br/>}< br/>
When const restricts pointer variables before asterisks, it restricts the objects pointed to by pointer variables. For the code in the above column, it restricts A and B so that, B cannot be re-assigned, so an error is reported during compilation: the const restricted object cannot be modified in the mian function. However, the values of the two pointer variables can be modified. Compile the following code:
/* Before cosnt *, you can change the value of the pointer variable */<br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> int A = 1, B = 2; <br/> const int * P1 = & A; <br/> int const * P2 = & B; <br/> printf ("A = % d/Nb = % d/N", * P1, * P2); <br/> P1 = & B; <br/> If (p1 = P2) <br/> printf ("the address of P1 is equal to the address of P2/N "); <br/> system ("pause"); <br/> return 0; <br/>}
So how to restrict pointer variables? The answer is to use the const after "*".
/* After cosnt *, the value of the pointer variable cannot be modified */<br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> int A = 1, B = 2; <br/> int * const p1 = & A; <br/> int const * P2 = & B; <br/> P1 = & B; <br/> system ("pause"); <br/> return 0; <br/>}
During compilation, an error is reported: Objects restricted by const cannot be modified in the mian function.
After thinking about the above Code, we will find that if we define this: const int * const A = & B; then we will find that the value of pointer variable A cannot be changed, the value of object variable B pointed to by pointer variable A cannot be changed.
In C ++:
Const in C ++ also plays a role in C. And the variable restricted by const can be used as a constant.
/* The change in the const limit can be used as a constant */<br/> # include <iostream> <br/> using namespace STD; <br/> int main (void) <br/>{< br/> const int A = 1; <br/> switch (1) <br/>{< br/> case: cout <"variable A can be used as a constant"; break; <br/> default: cout <"variable A cannot be used as a constant"; break; <br/>}< br/> system ("pause"); <br/> return 0; <br/>}
Note:
1. there are two types of annotations in C ++. One is a single line annotation -- //, which is written after; the other is multi-line annotation --/**/, which is inherited from C, as long as the comments are in "/*" and, whether or not annotations can be nested, we recommend that you do not use annotation nesting.
2. The above error was originally reported in English and translated by myself. Any improper translation may include the following.
The most common method of the const keyword is to use it as the parameter setting of the function. To prevent the value of the parameter from being changed, the const keyword is used for restriction. For example (C language ):
/* Const is usually used to modify the function Setting Parameter */<br/> # include <stdio. h> </P> <p> void max (const int A, int const B); </P> <p> int main (void) <br/>{< br/> int A = 1, B = 2; <br/> MAX (A, B ); <br/> system ("Pause> NUL"); <br/> return 0; <br/>}</P> <p> void max (const int, int const B) <br/>{< br/> A <B? Printf ("B is bigger than a/N"): printf ("A is bigger than B/N"); <br/>}
/* Const limits the return value of the function */<br/> # include <stdio. h> <br/> const int A (void) <br/>{< br/> int A = 1; <br/> return; <br/>}< br/> int main (void) <br/>{< br/> int B = (); <br/> printf ("B is % d/N", B); <br/> system ("pause"); <br/> return 0; <br/>}< br/>
Const reference
The concept of "Reference" is introduced in C ++. The so-called reference is actually just a name for a variable, this means that everyone has a learning name and a milk name. Although the names are different, they refer to the same person. It is worth noting that the reference must be initialized, and after the reference is initialized, as long as the reference exists, it will be bound to the object at the time of initialization, and cannot bind the reference to another object.
In addition, references are divided into non-const references and const references. Here we will talk about const references.
// Const reference can directly reference constants <br/> # include <iostream> <br/> using namespace STD; <br/> int B (void) <br/>{< br/> const Int & A = 1; <br/> return a; <br/>}< br/> int main (void) <br/>{< br/> int A = B (); <br/> const Int & C = 1; // invalid <br/> cout <A <Endl; <br/> cout <C <Endl; <br/> system ("pause "); <br/> return 0; <br/>}< br/>
You will find that this code will report an error, because the non-const reference in the main function directly references the constant, which is illegal, therefore, as long as you remove the reference and output of C, it can run normally. This is the difference between const reference and non-const reference.
// Reference cannot be referenced <br/> # include <iostream> <br/> using namespace STD; <br/> int main (void) <br/>{< br/> const Int & A = 1; // valid <br/> Int & B =; // invalid <br/> cout <A <Endl <B <Endl; <br/> system ("pause"); <br/> return 0; <br/>}< br/>
After you delete B from the program, the program can be compiled and run properly.
// Const reference can be initialized to different types of objects or the right value of initialization <br/> # include <iostream> <br/> using namespace STD; <br/> int main (void) <br/>{< br/> double A = 3.14; <br/> const Int & B =; <br/> cout <B <Endl <A <Endl; <br/> return 0; <br/>}< br/>
B here is invalid if it is not a const limit (C ++ primer can be referenced, and if B is modified, a will not be modified, in addition, B References A. In fact, there is an intermediate variable during compilation. The variable is modified, but I tried it on VC ++ and dev_c ++, and an error will be reported ).
These examples are very simple, and they are not very obvious to illustrate the problem. They are just a record of what I learned, just to demonstrate usage and hope to help readers.