c++ - Getting right value from an array within a class that behaves like matrix -
i've got class, behaves lika matrix, , operations there + , -, * etc work well.. when i'm trying single value, weird values 2.08252e-317 ... i've realized, bug happens only, when want create new metrix way:
const matrix b = ;
without const works well...
this declaration of class:
class matrix { public: matrix(int x, int y); ~matrix(void); matrix operator+(const matrix &matrix) const; matrix operator-() const; matrix operator-(const matrix &matrix) const; matrix operator*(const double x) const; matrix operator*(const matrix &matrix) const; class proxy { matrix* _a; const matrix* _consta; int _i; public: proxy(matrix& a, int i) : _a(&a), _i(i) { } proxy(const matrix& consta, int i) : _consta(&consta), _i(i) { } double& operator[](int j) { return _a->_arrayofarrays[_i][j]; } }; proxy operator[](int i) { return proxy(*this, i); } proxy operator[](int i) const { return proxy(*this, i); } // copy constructor matrix(const matrix& other) : _arrayofarrays() { _arrayofarrays = new double*[other.x ]; (int = 0; != other.x; i++) _arrayofarrays[i] = new double[other.y]; (int = 0; != other.x; i++) (int j = 0; j != other.y; j++) _arrayofarrays[i][j] = other._arrayofarrays[i][j]; x = other.x; y = other.y; } int x, y; double** _arrayofarrays; }; //constructor matrix::matrix(int x, int y) { _arrayofarrays = new double*[x]; (int = 0; < x; ++i) _arrayofarrays[i] = new double[y]; this->x = x; this->y = y; } so create instance, fill values , then, when want print value on index weird values :-/
ie:
matrix a(3,5); //fill in values cout << a[1][1] << endl // prints out nonsence does have idea, how rewrite class able proper values?
Comments
Post a Comment