首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我应该返回常量对象吗?

我应该返回常量对象吗?
EN

Stack Overflow用户
提问于 2012-08-21 16:36:00
回答 6查看 30.3K关注 0票数 79

Effective C++项目03中,请尽可能使用常量。

代码语言:javascript
运行
复制
class Bigint
{
  int _data[MAXLEN];
  //...
public:
  int& operator[](const int index) { return _data[index]; }
  const int operator[](const int index) const { return _data[index]; }
  //...
};

int& operator[]相比,const int operator[]确实有所不同。

但是如果这样呢:

代码语言:javascript
运行
复制
int foo() { }

代码语言:javascript
运行
复制
const int foo() { }

看起来它们是一样的。

我的问题是,为什么我们使用const int operator[](const int index) const而不是int operator[](const int index) const

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12051012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档