For the following function that represents a matrix
class m // matrix
{
private:
double **matrix;
int nrows, ncols;
class p
{
private:
double *arr;
public:
p (double *a)
: arr (a)
{
}
double &operator[] (int c)
{
return arr[c];
}
};
public:
m (int nrows, int ncols)
{
this->matrix = new double *[nrows];
for (int i = 0; i < nrows; ++i)
{
this->matrix[i] = new double [ncols];
}
this->nrows = nrows;
this->ncols = ncols;
}
~m()
{
for (int i = 0; i < this->nrows; ++i)
{
delete [] this->matrix[i];
}
delete this->matrix;
}
void assign (int r, int c, double v)
{
this->matrix[r][c] = v;
}
p operator[] (int r)
{
return p (this->matrix[r]);
}
};
operator works for element access but does not work with element change. How can I add the functionality of assign()
function to the operator?