</pre>1, const pointer </p><p><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "White-space:pre" > </span> if the const appears to the left of the "*", it means that the const modifier is pointing to the data, if it appears on the right, Note that the const modifier is the pointer, the constant pointer, which can no longer point to another place. </span></p><p><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "White-space:pre" > </span>stl iterators are also a special kind of pointer, if the form: </span></p><p ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span></span><pre name= "code" class= "CPP" >stl::vector <int> Vec;
Const Vector<int>::iterator IT = Vec.begin ();
it++; Error, the iterator can not change;
The const modifier is the iterator itself, which means that the iterator cannot change.
If you use const_iterator, it means that the data that the iterator points to cannot be changed, and the iterator itself can change.
2, the const member function,
(1) The const is part of the function type and is also to be taken in the implementation section.
(2) The Const keyword can be used to differentiate between overloaded functions. Because each member function has an invisible this pointer in front of it, and the const modifier is the this pointer, which means that the contents of this object cannot be modified.
(3) A constant member function cannot update a member variable of a class, but it can call a member variable and cannot invoke a member function in the class that is not decorated with const, and can only invoke a constant member function
The following example example of the Const keyword overload
Class a{
Private
int w,h;
Public
int GetValue () const
{
return w*h;
}
int GetValue () {
return w+h;
}
A (int x,int y)
{
W=x,h=y;
}
A () {}
};
void Main ()
{
A const a (3,4); The const function makes it possible to manipulate a const object.
A c (2,6);
Cout<<a.getvalue () <<c.getvalue () << "Cctwltest"; Output 12 and 8
System ("pause");
}