"Effective C + +" Inside said, as far as possible to use CONST,CONST modified variable generally there are two ways: const T *a, or T const *a, both are the same, mainly see the const is located on the left or right, here no longer repeat, Let's take a look at the member functions of the const-decorated class, what are the characteristics of the member function.
the member function of the class is followed by a const,indicates that this function does not make any changes to the data members of this class object (accurately, non-static data members).
when designing a class, one principle is to add a const to the member function that does not change the data member, and the member function that changes the data member cannot be const. Therefore, the Const keyword defines the behavior of the member function more explicitly:a const-Modified member function (that is, the const is placed after the function parameter table, not in front of the function or in the parameter table), can only read the data member, cannot change the data member, the member function without the const adornment, the data member is readable and writable.
Besides, what is the benefit of adding a const after the member function of a class? that is, a constant (that is, a const) object can call a const member function, not a non-const-decorated function.
Note: Two member functions can be overloaded if they are just a constant
For example:
Class A
{
Public
void F ()
{
cout<< "Non const" <<endl;
}
void f () const
{
cout<< "Const" <<endl;
}
};
Microsoft written The second question is exactly this, the const object calls F () const, non-const object call F ()
member functions for const-decorated classes