I have just reproduced an article "scenario analysis" const keywords in C Language ", and csdn also has a const usage summary. it may be too complicated for many people to despair about const after reading these two articles. In fact, const is not complex. It has rules. Otherwise, the syntax of the design language will be difficult (you can understand the compilation principles ). The following describes my understanding of const:
I think the keywords in the language all have a modifier field (that is, this keyword modifies the range) and a modifier direction (that is, starting from there ). For a language similar to C (C, C ++, Java), the modifier direction of the keyword is from the right, that is, the keyword does not modify the left side of the keyword. From the perspective of compilation principles, we can understand why this is the case. Now, the compiler basically uses LR (k) for syntax analysis, LR (k) in the analysis, K indicates the number of input strings to be viewed. If you have to look left during syntax analysis, it is not suitable for LR (k. A good understanding of the keyword modifier domain is where the keyword is modified. For languages similar to C (C, C ++, Java), the modifier domain is generally within the statement.
Let's take a look at const:
Const int max = 100;
The scope of const modification is int Max, where the variable is Max, which means max cannot be changed.
========================================================== ==================================
Int const max = 100;
The modification range of const is Max, and the variable here is Max, so it still means that Max cannot be changed. Here, we generally use static int I = 0; in fact, int static I = 0; is also valid.
========================================================== ==================================
Const int * A; // The modifier points to the object. A is variable. The modifier range of the object to which A points is variable. The modifier range of the const is int * A. The variable here is, therefore, you must use * A to reach the const modification level. So it represents A variable,
The object to which A points is unchangeable, that is, using * A = 10 is invalid.
=====================================================================
Int const * A; // modify the object to which A points. A is variable, and A is variable.
The scope of const modification is * A, where the variable is A, so you only need to use * A to reach the const modification level. So the meaning is the same as above.
=====================================================================
Int * const A; // modifier pointer A, A is immutable, and A points to the object is variable
The scope of const modification is A, where the variable is A, so const directly modifies A, that is, A reaches the const modification level, so A = & M is invalid.
=====================================================================
Const int * const A; // The objects pointed to by pointers A and A are not changeable.
The modification range of the previous const is int * const A, and that of the last const is A. Therefore, whether A or * A is used, it will reach the const modification level,
Therefore, the objects pointed to by pointers A and A are unchangeable.
=====================================================================
void fun0(const B* a );
The scope of const modification is B * a, so * a will reach the const modification level, so * a = c cannot be used in the function body;
=====================================================================
void fun1(const B& a);
The scope of const modification is B & a, so a will reach the const modification level, so a = c cannot be used in the function body;
=====================================================================
const B fun2( );
The scope of const modification is B, so the returned value reaches the const modification level, so the following code is invalid,
const B temp=fun2();
temp=c;
=====================================================================
const B* fun3( );
The modifier range of const is B *, so the value of the returned value reaches the const modifier level. Therefore, the following code is invalid,
const B* temp=fun2();
Temp = & c; // valid
* Temp = c; // invalid
=====================================================================
Class Stack
{
Public:
Void Push (int elem );
Int Pop (void );
Int GetCount (void) const; // const member function
Private:
Int m_num;
Int m_data [100];
};
Int Stack: GetCount (void) const
{
+ + M_num; // compilation error. An attempt is made to modify the data member m_num.
Pop (); // compilation error in an attempt to call a non-const Function
Return m_num;
}
Const modifies the function body of the class member method, so it means that the class member variables cannot be modified. I think this is also related to the compiler, because c ++ usually uses the this pointer as the last parameter during compilation (Some compilers will pass it as the first parameter ), therefore, the above Code can also be understood as const modifies the this pointer, so (* this ). m_mum) ++ has reached the const modification level, so an error is reported, the principle of calling the member method is the same (the bottom layer of the C ++ object to the compilation is actually similar to the structure of c. At first, the c ++ compiler first compiled it into c, then c is compiled into an executable program. You can search for cfront to understand this history ).
========================================================== ==================================
I think using the above analysis method, we can understand the keyword const very well. If there is any error, please point it out.