Home > Back-end >  CONST & newbie question
CONST & newbie question

Time:10-07

# include
# include
using namespace std;
Int main () {

Int a=0;
Const int& B=a;
Cout & lt; Cout & lt;

A=1;//to a assignment;
Cout & lt; Cout & lt;
b=1;//expression must be modifiable lvalue
Cout & lt; 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 56
of 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:

reference 1st floor qq_24037893 response:
c + + primer fifth edition on page 56
of 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,,,,,,,

Const limit is b, b is a read-only for a reference. That is to say by b can only read a data, not modify it. Don't change a property itself. This is just coming up from grammar and socialist standardize our programming, as for the other technical means to access memory, is beyond the scope of this!

CodePudding user response:

Const role is not to change, of course: const int the memory address of the variable of this type definition is not to change, but the value of the stored in the memory can be changed,

CodePudding user response:

Const is used at compile time.

CodePudding user response:

Const T is very interesting, everybody thought, for example, a const variable will be stored in the constant area, this is not entirely correct, when your program won't pick up address to const variable, the compiler may not save it, for example:
Const int b=10;
.
Fun (b);
The compiled code, may be
Fun (10);
Like I said above, const T work at compile time, only if you through some means, to bypass the compile time limits on const, by reference, for example, get a const variable address specific store, you can write the const variable,
  • Related