What begun as a revision of some aspects of C++ has become a sketch of something that could be useful for me. The idea is to use a single array as a double array where indexing is transparent for the user.
class matrice {
private:
int* data;
int rows, cols;
public:
matrice(int _rows, int _cols) : rows(_rows), cols(_cols) { data = new int[_rows*_cols]; std::cout << "matrice ctr" << std::endl;}
// hardcoded 2x2 matrix
void set_value() { data[0] = 1; data[1] = 0; data[3] = 1; data[4] = 0;}
int get_rows() { return rows;}
int get_cols() { return cols;}
class proxy {
private:
int i;
int j;
const int rows; // it is constant so we don't mess up with the size of the matrix
int *proxy_data;
public:
proxy(int* _proxy_data, int _i, const int& _rows) : proxy_data(_proxy_data), i(_i), rows(_rows) { }
int operator[] (int _j) { j = _j; std::cout << "proxy:\n\tj = " << j << " " << proxy_data[j] << std::endl; return proxy_data[i*rows + j];}
};
proxy operator[] (int i) { std::cout << "matrice:\n\ti = " << i << std::endl; return proxy(data, i, rows); }
};
int main()
{
int rows = 2;
int cols = 2;
matrice matp(rows, cols);
matp.set_value();
matp[0][0] = 2;
for (int i = 0;i < rows;i++) {
for (int j = 0;j < cols;j++) {
std::cout << "matp[" << i << "][" << j << "] = " << matp[i][j] << std::endl;
}
}
}
So far I can access the data in the array however, I want to assign values to it i.e.: matp[0][0] = 2;
How could I do it ?