When running the program, the data used must first be stored in the memory. Memory has two basic attributes: its address (number) and its stored data. Just like a pile of small boxes, numbers are used to identify which box is used, and values are like what is in the box.
The data is stored in the memory. We give it a name. The name is just a symbol, and the symbol itself is meaningless. After getting the name, you can retrieve the number based on the name. The name will be appended to the address. It can be said that the name is only for people, and that person is most likely you, so for yourself, but also for others' happiness, please take some time to get a good name.
After the data is stored in the memory, it can be divided into variables and constants. The constant value also remains unchanged. As mentioned above, the memory has two basic attributes: Address and stored data. Therefore, constants and variables also have two basic attributes, one is the memory address allocated to it, and the other is the value in the memory indicated by the address. Constants and variables are distinguished by the variation of values in the address. The program execution time value can be changed to a variable and cannot be changed to a constant. The constant value has been determined before the program is executed. Of course, variables and constants have other elements, such as names and types. The name will eventually be mapped to the address, and the type can determine their size and behavior. Type has its own meaning.
The values in the address can distinguish between variables and constants. From the memory address allocated, variables and constants can be divided into three different states: static, dynamic, and automatic. In C ++, the meaning of the keyword const is constant, and the meaning of the keyword static is fixed (static ). According to my understanding (note, it is my own understanding), constant and quiet, change and motion mean both change and not change, but the constant and variable values are for the number of memory storage, and the static and dynamic values are for the memory address.
Okay. Now let's take a look at what static, dynamic, and automatic means and how to differentiate them? When the program is just started, the system will allocate some stacks, program control blocks, various segments, and so on. This can be considered as a preparation phase. In this preparation phase, the program code has not been officially executed. The memory required for constants and constants has been allocated, and the address has been determined. This is static. When the program code has been executed, the memory is allocated. The memory address is not determined yet. It is dynamic or automatic. If the code is being executed and the memory needs to be allocated, this allocation will be completed by the system. You don't have to worry about it, it will be automatic. If you need the programmer to decide the allocation time, calling allocation functions such as malloc and new is dynamic. In summary, the memory has been allocated before the program is executed, and the memory address is determined to be static. When the program is executed, it is distributed in two cases. 1) the system is automatically completed, is automatic type. 2) You need to explicitly call the allocation function, and decide the time, which is dynamic.
The process of associating attributes to data is called binding ). If the attributes of the variables and constants have been determined before the program is executed, static binding is called. If the attributes are associated and determined only when the program is executed, dynamic binding. Looking at C ++ books, static, dynamic, and bound concepts will always appear. Here we should have a rough understanding, mainly to distinguish between attributes. In fact, it is not just a variable or a constant. Sometimes it is necessary to determine the function to call (that is, the function address) during program execution. At this time, you can first save the function address or make a table. Someone may ask, how can the function address be put up? Of course, the function also needs the memory. Since it is stored in the memory, in order to retrieve it, it must be returned to its location, that is, the address. For computers, everything is a numerical value such as 101010, and there is no difference in anything. In this case, functions, codes, floating points, and objects are no different from integer types such as Int. Int can be placed first. Why can't a function be?
========================================================== ======================================
In C ++, const is a very important keyword and imposes a constraint. Being restrained is not a bad thing. Endless rights mean endless disasters. After the const is applied, the value of the variable cannot be changed. If the compiler is accidentally changed, an error is reported, and you can easily find the error. Do not be afraid to report errors to the compiler. Just as you are not afraid to point out your shortcomings, the compiler is a friend of programmers. The more errors found during compilation, the fewer hidden errors. Therefore, as long as you think there is something unchanged, use the const modifier. The more you use, the better. For example, if you want to calculate the circle perimeter, you need to use pi. If PI does not change, add const and const double Pi = 3.1415926. For example, you need to reference in the function, read-only, it will not change. const is added before. For example, if a function has a return value, the return value is a reference, read-only, and will not change, and const is added before. For example, the class has private data, if the outside world needs to read data in the form of a function, it will not change. Add Const. In this case, it is added to the end of the function definition. Adding the end is just a syntax problem. In fact, you don't need to pay too much attention to the syntax. The syntax is just a short topic and you can't remember it. You just need to flip the book and get too touched. Naturally, you can remember that some concepts are hard to understand. If you think about it, the const is added to the front to modify the return value of the function. At this time, there is no place to put the const at the end.
However, you need to pay attention to the const modifier pointer. If the modified type is not a pointer, such as int, const is placed before int and after int, such
Const int A = 2;
Int const A = 2;
Has the same effect. I prefer the first method. In fact, the second method is more reasonable, indicating that the variable A itself is modified, so the value of A is immutable.
When the type is pointer, the circle is star *, and const is added to both sides of the left and right, which has different meanings.
1) const int * pA = & A; (you can write it as int const * pA = & A; note that it is bounded by an asterisk)
2) int * const Pa = &;
1) indicates the variable pointed to by PA, that is, the value of a is unchangeable. 2) indicates the direction of PA, that is, the value of PA itself cannot be changed. It cannot point to a now, followed by B.
========================================================
Int A = 2;
Int B = 3;
Const int * PT = &;
// * PA = 1; Error
Pa = & B; OK
============================================
Int A = 2;
Int B = 3;
Int * const Pt = &;
* PA = 1; OK
// Pa = & B; Error
========================================
As mentioned above, const is used to indicate that the value in the memory will not change. The pointer is essentially an address (number), which also needs to be placed in the memory. Therefore, the PA variable is stored in the memory, and the value is an address. When the const is on the right side of *, the const directly modifies the PA, indicating that the value of PA will not change, so the point cannot be changed. When the const is on the left side of *, the variable pointing to is modified. Therefore, the value of * pA cannot be changed. If the value of const int * const Pa = & A; A is immutable, the orientation of PA is also immutable. Think about it. This problem has been confusing for a long time.