First, the symbolic constant declarations
In the form of constant declaration statement: const + + constant specifier data type name = constant value
Data type specifier name + const + constant = constant value
Note: Be sure to assign symbolic constant initial statement, and can not change its value in the program.
const float PI = 3.14159; // The following statement is wrong const float PI; PI = 3.14159;
Two, const * and * const distinction
Understand the current form
For example: const int * p (int const * p)
int *const p
const int* const p
Meaning 1.const int * p (int const * p) of
Consistent with the meaning of the expression of both, i.e. a const * p, * p can not be modified to point p, it may call for the read-pointer
Because it is the point of data as constants, defined when you can not initialize
1. int a = 0;
const int* p;
p = &a;
* P = 2; // error, can not modify the value * p
2. int a = 0;
const int* p = &a;
a = 1;
cout << * p << endl; // At this point the value of p can be changed, as a printed
3. int a= 0,b = 1;
const int* p = &a;
cout << * p << endl; // * p is 0
p = &b;
cout << * p << endl; // * p is 1
2. int * const p meaning of
This definition const p as a constant, it is necessary to define the initialization, and the position of the point p can not be changed, so it can call pointer constant
Int = 0 A 1., B =. 1;
int const * = P & A;
P = & B; // wrong, p can not change the point
2. int a = 0, b = 1;
int* const p = &a;
*p = b;
cout << *p << endl; //*p 为 1
3. int a = 0, b = 1;
int*const p = &a;
a = b;
cout << *p << endl; //*p 为 1
(Const int * p and int * const p and p const int * const difference of)
const int * p p represents the variable pointed to as constants
int * const p p is defined to represent itself as a constant, it is necessary to define initialization
represents const int * const p p, * p are constants, i.e., p can not change the point, not by changing the value of p * p points
There is a commentary said very interesting, share it:
See "effective c ++" Article mentioned:
only need to determine const * is in the left or right can be.
Was modified left is assigned, i.e., is a constant referring to things, its value can not be modified;
the right is the modified pointer, i.e. the pointer is constant, it can not be modified point;
in the left and right sides, and pointers are constants were signans , can not be modified.
. 3 C = int;
int A = 2;
int = B. 1;
// const * appears in the left, is a constant referring to things were
const int & pi = A *;
* pi = B; // was incorrectly assigned is constant
pi = & c; // correct
// const * appears in the right side, the pointer is constant
int const * = P & A;
P = & c; // incorrect, the pointer is constant
* p = c; // correct
// const appear * left and right sides, and pointers are constants were signans
const int const * & PTR = A;
PTR = & C; // not correct, is a constant pointer
* ptr = c; // incorrect, was alleged to be constant
Precautions!
1. If const int a = 0; it must be a const int * to point to a, like int * p = & a; is illegal
2.const int a =10;
int * p = & a; // error, which can not take the address, otherwise there is the ability to modify the value of
The above is the learning stage record, correct me if wrong hope :)