index operator []重载 [英] index operator[] overloading
问题描述
任何人都可以解释这是如何工作的。我该如何实现呢?
我希望能够在分配的lhs和rhs上使用运算符
语句。 rhs是不费脑筋的(至少我认为我做对了):
class MyArray
{
public :
MyArray(const size_t size):m_size(size){arr_ = new double [size]; }
~MyArray(){if(arr_)delete [] arr_;}
double operator [](const size_t idx)
{if(idx< m_size)
return arr_ [idx];
throw std :: exception
}
私人:
MyArray(const MyArray&);
operator =(const MyArray&);
double * arr_;
size_t m_size;
}
我在某处读到[]运算符返回的类型必须是一个
参考,但我没有完全按照推理/逻辑 - 任何人关心
来解释?
Can anyone explain how this works. How may I implement this?
I want to be able to use the operator on both lhs and rhs of assignment
statements. The rhs is a no-brainer (at least I think I got it right):
class MyArray
{
public:
MyArray(const size_t size):m_size(size){ arr_ = new double[size]; }
~MyArray(){ if (arr_) delete[] arr_ ;}
double operator[](const size_t idx)
{ if (idx < m_size)
return arr_[idx];
throw std::exception
}
private:
MyArray(const MyArray&);
operator= (const MyArray&);
double *arr_ ;
size_t m_size ;
}
I read somewhere that the type returned from the [] operator must be a
reference, but I did not quite follow the reasoning/logic - anyone care
to explain ?
推荐答案
Bart Simpson写道:
Bart Simpson wrote:
>
我在某处读到了从[]运算符返回的类型必须是一个
参考,但我没有完全按照推理/逻辑 - 任何人关心
来解释?
>
I read somewhere that the type returned from the [] operator must be a
reference, but I did not quite follow the reasoning/logic - anyone care
to explain ?
引用是与另一个变量相关的变量。例如
int x = 0;
int& y = x;
y = 1;
最后
,x的值为1,因为y指的是相同的
内存位置为x,所以它绑定到x值,直到它的
范围结束(希望是在x之前或与x一起的时间:): />
在你的情况下它是完全相同的:如果你返回一个引用,你就是
返回一个(临时)变量引用的地址
要修改的
元素。所以,如果你给这个
变量赋值(如果它不是引用就会被禁止,因为暂时是因为这是暂时的),这个值将被写入你的
数组元素的内存位置。
就是这样!
问候,
Zeppe
A reference is a variable that relates to another variable. for example
int x = 0;
int& y = x;
y = 1;
at the end, x will have a value of 1, because y refers to the same
memory location as x, so it''s bind to the x value until the end of its
scope (that hopefully is before or at the same time as the x one :)
In your case it''s exactly the same: if you return a reference, you are
returning a (temporary) variable that refers to the address of the
element that you want to modify. So, if you assign a value to this
variable (that would be prohibited if it wasn''t a reference, because is
a temporary), the value will be written in the memory location of your
array element.
that''s it!
Regards,
Zeppe
谢谢Zeppe澄清了这一点。
所以要实现它实现这两个?
myType operator [](const size_t)const;
myType& operator [](const size_t);
Thanks Zeppe that clarifies that.
So to implement it one has to implement BOTH of these?
myType operator[](const size_t) const;
myType& operator[](const size_t);
Bart Simpson写道:
Bart Simpson wrote:
谢谢Zeppe澄清了这一点。 br />
所以为了实现它,我们必须实现这两种方式吗?
myType operator [](const size_t)const;
myType& operator [](const size_t);
Thanks Zeppe that clarifies that.
So to implement it one has to implement BOTH of these?
myType operator[](const size_t) const;
myType& operator[](const size_t);
确切地说,编译器将根据
" const"选择合适的一个。另一个提示:方法
myType operator [](const size_t)const;
生成它返回的元素的副本,如果
对象myType很大,则可能是不受欢迎的。所以,它通常很常见
const myType& operator [](const size_t)const;
myType& operator [](const size_t);
如果你返回一个const引用,你就不能在
的左边使用它,并且你在回程上避免复制。
问候,
Zeppe
exactly, and the compiler will chose the proper one based on the
"const". Another hint: the method
myType operator[](const size_t) const;
makes a copy of the element that it returns, which can be undesirable if
the objects myType are big. So, it usually common to do
const myType& operator[](const size_t) const;
myType& operator[](const size_t);
if you return a const reference, you can''t use it in the left side of
the assignment, and you avoid the copy on the return.
Regards,
Zeppe
这篇关于index operator []重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!