c++ - Supporting my_2D_Array[x][y] (double operator[]) on a const 2D array; Cannot get things working -
c++ - Supporting my_2D_Array[x][y] (double operator[]) on a const 2D array; Cannot get things working -
i creating 2d array class in c++. works fine non-cost array. have yet find explanation of const functions, , seem fumble around until works. now, however, @ impass.
am not understanding how back upwards const objects? short of creating sec row class, can't think of solution, , solution seems unnecessary.
main.cpp
#include "array2d.h" int main() { int const x(1), y(1); array2d<int> arr(3, 3); int value(0); (int row(0); row < 3; ++row) (int column(0); column < 3; ++column) arr[row][column] = ++value; std::cout << arr[x][y]; array2d<int> const carr(arr); //everything's fine here std::cout << carr[x][y]; //error std::cout << "\n\n[ pause: press come in exit program. ]"; std::cin.get(); homecoming 0; }
when seek access carr[x][y] throws error, expected. included const , non-const overload of operator[] in array2d
array2d.h
template <typename t> class array2d { public: //=============== //constructors // inline array2d(); inline array2d(int rows, int columns = 0); inline array2d(array2d const &array2d); //=============== //operators // inline array2d<t>& operator=(array2d const &array2d); inline row<t> operator[](int index); inline row<t> const operator[](int index) const; //=============== //getters // inline int getrows() const { homecoming _rows; } inline int getcolumns() const { homecoming _columns; } //=============== //setters // inline void setrows(int rows); //may result in loss of info inline void setcolumns(int columns); //may result in loss of info //other inline t& select(int row, int column); inline t const & select(int row, int column) const; //=============== //destructor // inline ~array2d(){} private: //=============== //data // array<t> _array; //two dimensional array in row-major order int _rows; //the number of rows in array int _columns; //the number of info in each row //=============== //private functions // //initializes array current info inline void initarray2d(); //throws exception if length negative inline void checklength(int length) const; //throws exception if index out of bounds inline void checkindex(int rowindex, int columnindex) const; }; //constructors template <typename t> array2d<t>::array2d() : _array(0), _rows(0), _columns(0) { initarray2d(); } template <typename t> array2d<t>::array2d(int rows, int columns) : _array(0), _rows(rows), _columns(columns) { checklength(rows); checklength(columns); initarray2d(); } template <typename t> array2d<t>::array2d(array2d const &array2d) { (*this) = array2d; } //operators template <typename t> array2d<t>& array2d<t>::operator=(array2d const &array2d) { _rows = array2d._rows; _columns = array2d._columns; initarray2d(); _array = array2d._array; homecoming *this; } template <typename t> row<t> array2d<t>::operator[](int index) { homecoming row<t>(*this, index); } template <typename t> row<t> const array2d<t>::operator[](int index) const { row<t> const toreturn(*this, index);; homecoming toreturn; } //setters template <typename t> void array2d<t>::setrows(int rows) { _array.setlength(rows * _columns); } template <typename t> void array2d<t>::setcolumns(int columns) { array newarray(_rows * columns); //this prevent truncated columns beingness copied on int lessercolumn(columns > _columns ? _columns : columns); (int row(0); row < _rows; ++row) (int column(0); column < lessercolumn; ++column) newarray[row][column] = _array[row][column]; _array = newarray; } //other template <typename t> t& array2d<t>::select(int row, int column) { checkindex(row, column); homecoming _array[(row * column) + column]; } template <typename t> t const & array2d<t>::select(int row, int column) const { checkindex(row, column); homecoming _array[(row * column) + column]; } // private \\ template <typename t> void array2d<t>::initarray2d() { _array = array<t>(_rows * _columns); } template <typename t> void array2d<t>::checklength(int length) const { if (length < 0) throw exception("length_less_than_zero"); } template <typename t> void array2d<t>::checkindex(int rowindex, int columnindex) const { if (rowindex >= _rows || columnindex >= _columns) throw exception("index_larger_than_upper_bound"); if (rowindex < 0 || columnindex < 0) throw exception("index_smaller_than_lower_bound"); }
the const functions seem in order, issue arises when tries create row object (explained in next paragraph) because expects array2d&, in const function passes const array2d&, naturally not work.
the thought of row object pass handle sec [] operator without having pass array.
row.h
template <typename t> class array2d; template <typename t> class row { public: //============== //constructor // inline row(array2d<t> &array2d, int row); //============== //operators // //the index validated array2d inline t& operator[](int column); inline t const & operator[](int column) const; private: //============== //data // array2d<t>& _array2d; //source array int _row; //the row index }; template <typename t> row<t>::row(array2d<t> &array2d, int row) : _array2d(array2d), _row(row) { } template <typename t> t& row<t>::operator[](int column) { homecoming _array2d.select(_row, column); } template <typename t> t const & row<t>::operator[](int column) const { homecoming _array2d.select(_row, column); }
here array.h reference
#pragma 1 time #include "exception.h" template <typename t> class array { public: //=============== //constructors // inline array(); inline array(int length, int startindex = 0); inline array(array const ©); //=============== //operators // inline array& operator=(array const ©); inline t& operator[](int index); inline t const & operator[](int index) const; //=============== //getters // inline int getstartindex() const { homecoming _startindex; } inline int getlength() const { homecoming _length; } //=============== //setters // inline void setstartindex(int index); inline void setlength(int length); //=============== //deconstructor <coolface.jpg> // inline ~array(); private: //=============== //data // t* _array; //pointer array of type t int _length; //length of array int _startindex; //=============== //private functions // //initializes array current length inline void init(); //throws exception if length less 0 inline void checklength(int length) const; //throws exception if index out of bounds inline void checkindex(int index) const; //copies contents of source destination inline void copyarray(t * destination, t * source, int lastindex); }; //constructors template <typename t> array<t>::array() : _array(0), _length(0), _startindex(0) { init(); } template <typename t> array<t>::array(int length, int startindex = 0) : _array(0), _length(length), _startindex(startindex) { checklength(length); init(); } template <typename t> array<t>::array(array const ©) { (*this) = copy; } //operators template <typename t> array<t>& array<t>::operator=(array const ©) { _length = copy._length; _startindex = copy._startindex; init(); copyarray(_array, copy._array, _length); homecoming *this; } template <typename t> t& array<t>::operator[](int index) { checkindex(index); homecoming _array[index - _startindex]; } template <typename t> t const & array<t>::operator[](int index) const { checkindex(index); homecoming _array[index - _startindex]; } //setters template <typename t> void array<t>::setstartindex(int index) { _startindex = index; } // ! warning: setting length lower value current result in lost info template <typename t> void array<t>::setlength(int length) { checklength(length); t* oldarray(_array); int oldlength(_length); _length = length; init(); int lastindex(oldlength < _length ? oldlength : _length); copyarray(_array, oldarray, lastindex); delete [] oldarray; } //deconstruct <coolface.jpg> template <typename t> array<t>::~array() { delete [] _array; } // private \\ template <typename t> void array<t>::init() { _array = new t[_length]; } template <typename t> void array<t>::checklength(int length) const { if (length < 0) throw exception("length_less_than_zero"); } template <typename t> void array<t>::checkindex(int index) const { if (index - _startindex >= _length) throw exception("index_larger_than_upper_bound"); if (index - _startindex < 0) throw exception("index_smaller_than_lower_bound"); } template <typename t> void array<t>::copyarray(t * destination, t * source, int lastindex) { while (lastindex--) destination[lastindex] = source[lastindex]; }
sadly, think you'll need 2 versions of row
: 1 const array
, 1 non-const. standard containers have iterator
s , const_iterator
s.
c++ multidimensional-array const
Comments
Post a Comment