常函数:
- 成员函数后加 const 后 , 我们称这个为 常函数
- 常函数内不可以修改成员属性
- 成员属性声明加 关键词: “ mutable “之后 , 就可以在成员函数中修改属性
常对象:
- 声明对象前加 const 为常对象
- 常对象只能调动常函数
Student{int age;mutable int height;void setAge(int a) const{age = a; //false; 常函数的属性不能修改height = a; //true}void getAge(){};}int main(){const Student stu;stu.setAge(10); //truestu.getAge(); //false; 常对象只能调用常函数}
