小编nop*_*nto的帖子

为什么使用const成员函数?

我正在努力理解课堂上的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)

真正的区别是什么?

c++ const class function

5
推荐指数
2
解决办法
6689
查看次数

标签 统计

c++ ×1

class ×1

const ×1

function ×1