B
Bart Simpson
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 ?
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 ?