很久不接触C++了,工作需要,很多东西都忘记了,只好把一些细小的知识点记录在这里帮助自己记忆。
Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. A constant member function cannot modify any data members or call any member functions that aren't constant. 从上面的说明来看,constant member function 有两个方面,一是它不能改变对象的非const数据成员,也不能调用对象的非const方法。二是,它可以改变对象的const数据成员[注意const数据成员本来是不可以改变的],可以调用对象的const方法。
1、const放在最后,表示此成员函数不改变类中的成员变量。 2、const放在函数参数中,表示此参数在函数体不会被改变。 3、const放在最前,表示返回const值