Article Source: http://topic.csdn.net/u/20070924/22/75c81814-1632-40c0-998b-ab876bd81558.html
What is the meaning of adding const to the member functions of the class?
Add const to the implicit this pointer of the member function to indicate thisPoint to somethingConst, that isThe data member cannot be modified in this function.. Const is a guarantee that this Member will not change the object state.
There are the following class declarations:
Class pig
{
Long _ lx;
Public:
Pig (const long & _ x = 9): _ lx (_ lx ){}
Void show (void)
{
Cout <"pig member _ lx:" <_ lx <Endl;
}
};
The compiler adds a concealed this pointer to the show () function in the preceding Declaration. Its declaration is similar to the following:
Void show (pig *ConstThis )//(This pointer is a const pointer of this type.)
{
......
}
In this way, after the class is instantiated as an object variable, show can refer to its data member _ lx with the above this pointer!
However, when a class is instantiated as a regular object, the data member of the class has a constant attribute, so that the this pointer can no longer refer to its class member _ lx, because the language rules do not allow constant access with a very-type pointer! For example:
Class pig
{
Long _ lx;
Public:
Pig (const long & _ x = 9): _ lx (_ x ){}
Void show (void) // No const modifier added !!!
{
Cout <"pig member _ lx:" <_ lx <Endl;
}
};
Int main ()
{
Const pig _ P;
/* The following "_ p. Show ();" statement is generated:
Errorc2662: pig: Show ": the" this "pointer cannot be converted from" const pig "to" pig &"
*/
_ P. Show ();//! Cannot pass compilation!
_ Pause;
Return 0;
}
To solve the preceding problem, you can declare the show () member function as a regular member function. In this way, the compiler inserts a new this pointer for the show () function. Similar declarations are as follows:
Void show (const pig * const this) const //!!!
{
......
}
This new this pointer will be able to access data class members including constants!
For example:
Class pig
{
Long _ lx;
Public:
Pig (const long & _ x = 9): _ lx (_ x ){}
Void show (void) const // declare as a regular member function!
{
Cout <"pig member _ lx:" <_ lx <Endl;
}
};
Int main ()
{
Const pig _ p; // declare as a regular object!
_ P. Show (); // good behavior! Output "pig member _ lx: 9 "!
_ Pause;
Return 0;
}
The above is why const is added to the member function!
PS.Mutable keyword (C ++)
Http://www.cnblogs.com/cheney23reg/archive/2010/08/13/1799279.html)
If you need to modify the value of a member variable in the const member method,Modify the member variable to mutable.. Mutable-modified member variables are not restricted by the const member method.
Mutable variables can be considered to be auxiliary states of the class, but they only serve as expressions of some aspects of the class. We can modify the content of this variable and think that the object State itself has not changed.In fact, due to the existence of const_cast, this concept is not very useful in many times..