const indicates that the object it modifies cannot be changed, such as constants, functions, pointers, etc., summarizing the common
several const usage:
     1 Pointer constants and constant pointers : These two concepts are easy to confuse, and each time I use these two > You need to look at the concept again.
documentation is assured, When viewing definitions and instances, very easy to understand the meaning and difference between the two , a long time, the concept is blurred again. Here
introduction of a simple notation, This method is seen in the third version of <<effective c++>>:
constant pointer To change the value of the pointer itself, you cannot
change the value of the object pointed to by the pointer , define the constant pointer , the const keyword should appear to the left of the asterisk, for example:
int a = 10;int b = 20; INT&NBSP;CONST&NBSP;*&NBSP;P&NBSP;=&NBSP;&A;&NBSP;&NBSP;//constant pointer, const to the left of the asterisk *p = 20; //error cannot modify the value of the object pointed to by the p = by dereferencing the pointer &b; //ok You can change the value of the pointer itself
defines constant pointers, The const can appear either on the left side of the type or in the class
is equivalent to:
const int * p1; const INT CONST * P2 on the left of int; Const on the right of int
pointer constant define
To the right of the number, for example:
int a = 10;int b = 20; int * const p = &a; //pointer constant, const to the right of the asterisk *p = 20; //ok The value of the object pointed to by the pointer can be changed p = &b; //error the value of the pointer itself cannot be changed
2 Declares iterators for const and const iterators : Declares an iterator as const as a declaration pointer
t* const pointer , indicating that the value of the iterator itself cannot be to change, no longer point to something else, but the value of the object it points to
can be changed For example:
Const Std::vector<int>::iterator ITER = Vec.begin () iter++; Error*iter = 10; Ok
and if you want the iterator to point to something that cannot be changed, the iterator itself can change That you want its effect to be like a const t* pointer,
Then you need a const iterator, const_iterator, for example:
Std::vector<int>::const_iterator citer = Vec.begin (); citer++; Ok*citer = 10; Error
&NBSP;&NBSP;&NBSP;&NBSP; 3 Const member function: const member functions are declared and defined with a const modifier, such as Two member functions only because
Unlike the Const property, these two functions can be overloaded , such as the following two functions can exist in a class at the same time:
String GetName () const; Const member function string GetName (); Non-const member functions
Non-const objects can call either a const member function or a non-const member function, If there is a
with this overload above
const member functions and non-const members function, non-const
member function, because to invoke a non-const member function, its value may be modified.
Summary of const usage in C + +