【发布时间】:2017-03-01 11:30:41
【问题描述】:
我正在尝试理解类 getter 和 setter 函数...
我的问题是: 如果我设计一个仅从其类中获取状态的函数(“getter”函数),为什么将其标记为“const 成员函数”? 我的意思是,如果我的函数旨在不更改其类的任何属性,为什么还要使用 const 成员函数? 我不明白请:(
例如:
int GetValue() {return a_private_variable;}
和
int GetValue() const {return a_private_variable;}
真正的区别是什么?
【问题讨论】:
标签: c++ function class constants