(Do not know why the title write C + + always does not appear, should be csdn new Markdown editor Bug) C + + const and C language #define can be used to define constants, the two are different, const is a constant of data types, and macro constants do not, The compiler can perform static type security checks on the former, which is only character substitution and no type security check.
The C language of the Const and C + + is also very different, in C language with the const modified variable is still a variable, indicating that the variable is read-only, can not be displayed changes, and in C + + after the use of the const modifier, it becomes a constant. For example, the following code:
const int n=10;
int a[n];
These two lines of code will have an error in the C language because the length of the array when declaring the array must be constant, and the n inside this is a variable in the C language. After using the const modifier in C + +, n is already equal to a constant, so it can be passed.
Then look at the following code:
const int a=3;
int* p=&a;
*p=4;
This is not allowed in C + + because A is a constant after a const modifier, so it is not allowed to be modified, either by explicitly changing the value of a or by modifying its value by other means.