c++ - Issue with Assignment Operator and Array Subscription operators -
i trying overload subscription operator , faced issue
 example class e, did @ first is:
int e::operator[](int n){     if(n<length && n>0)         return data[n];     else         return 0;  }   let have object of e ( ), , want return a[0]. operator works fine.
 second thing wanted if want a[0] = 4. 
 need implement here? assignment operator? or subscription operator? 
 advice how that, thanks!
commonly subscript operator written 2 overloads: 1 read , 1 write. allows read in const contexts:
write overload:
int& e::operator[](size_t index) {     if( index >= lenght )         throw std::out_of_range("subscript out of range");     else         return data[n]; }   read overload: (note const cualified)
int e::operator[](size_t index) const {     if( index >= lenght )         throw std::out_of_range("subscript out of range");     else         return data[n]; }      
Comments
Post a Comment