#define是个真常量, at compile time, the name of the macro in the code will be replaced. A const constant is a false constant, and a variable defined with a const is ultimately a variable, only checked within the compiler, and an error is found if it is modified. Since the const-modified variable nature has not changed, then it can be modified, and here does not say its usefulness. (Other cases of const, such as data members in a decorated class, member functions, etc., discussed later)
intMain () {intI=1; Const intb=2; Const int*p1=&i;//The value of I is modified by the pointer P1, but the value of P1 can be changed, that is, the P1 can be re-assigned int Const*p2=&i;//exactly the same as P1 . int*Constp3=&i;//P3 cannot point to a const object and cannot reassign a value to P3, but can modify the value of I by P3. Const int*Constp4=&i;//You cannot modify the value of I through *P4, nor can I reassign a value to P4. Const int&p5=i;//The reference must be initialized, cannot be re-assigned, and the value of I cannot be modified here by P5. int *p= (int *) &b;//You cannot assign a const object to a non-const pointer,//But you can cast a const object, remove the const adornment, and modify the data of the const object *p=20;//Modifying the value of const int Bcout<<b<<Endl; System ("Pause"); return 0;}
The value of the output B is still 2, but it does not mean that B cannot be modified, except that the compiler found B to be const-decorated during compilation, and then all the places that use B are replaced with their constant values. So Cout<<b<<endl is optimized during compilation to
cout<<5<<endl;
By debugging, you can see that the value of const constant B is modified by the pointer p.
intI=1; 00f813be mov dword ptr [i],1 Const intb=2; 00F813C5 mov dword ptr [b],2 Const int*p1=&i;//The value of I is modified by the pointer P1, but the value of P1 can be changed, that is, the P1 can be re-assigned00f813cc Lea Eax,[i] 00F813CF mov dword ptr [P1],eaxint Const*p2=&i;//exactly the same as P1 .00f813d2 Lea Eax,[i] 00F813D5 mov dword ptr [P2],eaxint*Constp3=&i;//P3 cannot point to a const object and cannot reassign a value to P3, but can modify the value of I by P3. 00f813d8 Lea Eax,[i] 00f813db mov dword ptr [P3],eaxConst int*Constp4=&i;//You cannot modify the value of I through *P4, nor can I reassign a value to P4. 00f813de Lea Eax,[i] 00F813E1 mov dword ptr [P4],eaxConst int&p5=i;//The reference must be initialized, cannot be re-assigned, and the value of I cannot be modified here by P5. 00f813e4 Lea Eax,[i] 00f813e7 mov dword ptr [P5],eaxint*p= (int*) &b;//A const object cannot be assigned to a non-const pointer,00f813ea Lea Eax,[b] 00f813ed mov dword ptr [P],eax//However, you can force type conversions on const objects, remove const adornments, and modify the data of Const objects*p= -;//Modify the value of the const int B 00f813f0 mov eax,dword ptr [P]//Remove the address of B from the pointer p to EAX, and then store 20 to eax that is the address of B 00F813F3 mov dword ptr [E ax],14h cout<<b<<ENDL;00F813F9 mov esi,esp 00F813FB mov eax,dword ptr [__imp_std::endl (0f882a4h)] 00f81400 push EAX 00f81401 mov edi,esp 00f81403 push 2 //You can see that the push is 2, not the data in the B address, which is the result of constant optimization (constant propagation)
#define, const These two types will no longer exist after the connection generates an executable file.
(c + + Disassembly Series) constants (Lesson two)