Differences between pointer constants and constant pointers
Int a, B;
Int * const p1 = & a; // p1 is a pointer constant.
Int const * p2 = & B; // p2 is a constant pointer.
Recognize constant pointers and pointer constants. Remember the following three sentences:
Pointer (*) and constant (const) who read before;
Pointer (*) before (int * const p1 = & a) indicates that the pointer cannot be changed and cannot be directed to another address, but the value of the indicated address can be changed through the pointer;
A constant (const) before (int const * p2 = & B) indicates that the value of the indicated address cannot be changed through the pointer, but the pointer can point to other places.
Example:
# Include <iostream> using namespace std; int main () {int a = 1; int B = 2; int const * p1 = & B; // constant pointer p1 cout <"* p1 =" <* p1 <endl; // p1 points to B, and the output value p1 = &; // you can change the p1 pointer of a constant to point to another location. cout <"* p1 =" <* p1 <endl; // p1 now points to, the output value of a is int * const p2 = & B; // pointer constant p2 cout <"* p2 =" <* p2 <endl; // p2 points to B, output the value of B * p2 = 3; // you can use the pointer constant p2 to change the value of the indicated object cout <"* p2 =" <* p2 <endl; // p2 also points to B, but the value of B changes to 3 return 0 ;}
2.Returns a constant's function.Can be a constant pointer, pointer constant, constant, form:
Const type * funcname (type1 arg1, type2 arg2 ,..)
Type * const funcname (type1 arg1, type2 arg2 ,..)
Const funcname (type1 arg1, type2 arg2 ,..)
3. Constant pointers and pointer constants are assigned values so far.
Both constant pointers and pointer constants can be assigned to constant pointer objects. Constant pointer objects can be p ++, but cannot be changed through * p.
Constant pointers and pointer constants can be assigned to pointer constants, but pointer constants can only perform * p operations, not p ++ operations.
A normal type of function that returns constants. The purpose is to prevent calculation between the return values of a member function from generating ugly code.