我正在努力理解课堂上的getter和setter功能......
我的问题是:如果我设计一个只从其类中获取状态的函数("getter"函数),为什么将其标记为"const成员函数"?我的意思是,如果我的函数被设计为不改变其类的任何属性,为什么使用const成员函数?我不明白请:(
例如:
int GetValue() {return a_private_variable;}
Run Code Online (Sandbox Code Playgroud)
和
int GetValue() const {return a_private_variable;}
Run Code Online (Sandbox Code Playgroud)
真正的区别是什么?