When looking at the open source code, often see in the class member function after add const, before have not too concerned about, recently idle to have no matter, think of this matter, on-line check a bit, probably understand is how, here quoted Csdn 愽 paragraph words: "The compiler will automatically give each function plus a this pointer." After adding a const to a function of a class, it is indicated that the function cannot change the member variables of the class (except with the addition of the mutable modifier). In fact, that is, the this pointer is added a const modifier ". As an example:
Class Test
{
Public
Test (int a=10): AA (a) {}
~test () {}
int Geta () const {Aa++,bb++;return AA}
Private
int AA;
mutable int BB;
};
int main ()
{
Test T (100);
int cc = T.geta ();
};
Here the compiler will error, C2166: The lvalue Specifies the const object, the Geta () function cannot perform the aa++ operation, but it is possible to execute bb++.
Add a const after a class member function