# include
using namespace std;
Int main () {
Int a=0;
Const int& B=a;
Cout & lt;
A=1;//to a assignment;
Cout & lt;
b=1;//expression must be modifiable lvalue
Cout & lt;
system("pause");
return 0;
}
Expressed doubt me as a beginner, b as a reference to the alias, just want to the same memory address 0 d9fc84, why can a change the data values stored in the memory address, however b as a const alias, but can't change the memory address of the stored data value I personal guess is that several problem:
1. The variable names and the relationship of the corresponding memory address I completely understand,,,,,,,,,
2. Whether a memory read-only CONST can change,,,,,,,,,,,
Novice Posting, teach more!
CodePudding user response:
C + + primer fifth edition on page 56of const reference may not a const reference object
Must realize that const reference only made a limited to the operation of the reference can participate in, to refer to the object itself is not a constant is not qualified, because the object may also is a very, so allow through other CONST& The way to change its value
You mean like the above questions, I can through a change in the value of data in memory address, does not allow through b
Personally, I find the answer to this, but I more wondering what on earth is CONST, that is the so-called pit c + +? Good deep,,,,,,,
CodePudding user response:
Your application is not in direct access to your memory,Your program by compiler into machine language, really access memory is the machine language instructions,
What you think even access to the "memory" (in quotes here, because c + + language in semantic memory and real memory is different, this refers to the former) statement, through the translation of the compiler, in machine language may correspond to look completely unrelated instruction,
Const is a c + + language level limit, not the limit on the level of access memory,
CodePudding user response:
Upstairs said some truth, learning c + +, the most main is to realize that you write const, int is a symbol of the compiler, rather than actually memory space, when you add const, tells the compiler is not allowed to change the value of this variable, so although b is a nickname, but you tell the compiler that the alias is immutable, so they don't allow you to go at compile time to assignment of the alias,CodePudding user response:
Don't be entangled with a variety of constants, the only constant is change in the world, with API WriteProcessMemory can also modify the other process is running inside the memory of the so-called constant!CodePudding user response:
Const qualifier is the meaning of "not allowed on this symbol assignment again", not "guarantee the memory of the two values"Compiler will only tube do you have a symbol of const modifiers modified, as to what is happening in the real memory, it won't know
The main role of this qualifier is told that the programmer, this is a can't change, prevent the programmer hand slide changed can't move things
This is not a hole, it is so
CodePudding user response: