For most people, the const usage, such as the modified return value and the modifier parameters, should be very well understood and I'm going to discuss
For the C language
#include <stdio.h>void main () {const int A=10;int *p = (int*) &a;*p = 100;printf ("%d\n", a);}
Top Output 100
For C + +,
#include <iostream>using namespace Std;void main () {const int A=10;int *p = (int*) &a;*p = 100;printf ("%d\n", a);}
Top Output 10
What is the reason, C is a const is a variable can not be changed, there is memory space allocation, so you can change in memory through the address
The const in C + + is a constant, which is usually stored in the character table, not allocating memory, but reassigning a variable with the same name as the const constant at the time of bearing the variable or looking at the address of the variable, so the address of the output at this time is the address of the new variable. So the value of this variable is changed by address, but the compiler will read the value of the constant in the symbol table, so the value does not change.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The const of thinking in idle time