返回信息流请教大家一个问题:c++primer第四版中关于“[]”操作符重载有如下说明
The following class defines the subscript operator. For simplicity, we assume the data Foo holds are stored in a vector<int>:
class Foo {
public:
int &operator[] (const size_t);
const int &operator[] (const size_t) const;
// other interface members
private:
vector<int> data;
// other member data and private utility functions
};
The subscript operators themselves would look something like:
int& Foo::operator[] (const size_t index)
{
return data[index]; // no range checking on index
}
const int& Foo::operator[] (const size_t index) const
{
return data[index]; // no range checking on index
}
书上说两个重载操作符定义一个用生成于左值,是非const的,一个用于右值,是const的
最后这两个定义明显是重复声明啊,参数一样,区别只有返回值是否为const,还有编译器是怎样知道我们调用的是左值还是右值呢?
谢谢[em13]
这是一条镜像帖。来源:北邮人论坛 / cpp / #14220同步于 2008/10/17
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
【求助】c++ []操作符重载问题
wangzhe123
2008/10/17镜像同步2 回复
订阅后,新回复会通过你的通知中心匿名送达。