我有一个简单的类
class Array
{
public:
Array();
~Array();
// Dereferencing operators
int operator[](std::size_t index) const;
int& operator[](std::size_t index);
}
我的问题是,在什么条件下调用int operator[](std::size_t index) const
?如何强制C++调用int operator[](std::size_t index) const
而不是int& operator[](std::size_t index)
?如果我只实现其中一个操作符,会发生什么?
我的问题是,在什么条件下调用int运算符[](std::size_tindex)const?
当在常量的Array实例上调用时
如何强制C++调用int运算符[](std::size_t index)const而不是int&运算符[](std::size_t索引)?
将数组的可变实例强制转换为常量引用
如果我只实现其中一个操作符,会发生什么?
如果只实现const one,则无法使用operator[]
写入下标。
如果只实现非常量实例,则无法读取数组的const实例的下标。