I have a 2D array and I want to define a function that returns the value of the index that the user gives me using operator overloading.
In other words:
void MyMatrix::ReturnValue()
{
int row = 0, col = 0;
cout << "Return Value From the last Matrix" << endl;
cout << "----------------------------------" << endl;
cout << "Please Enter the index: [" << row << "][" << col << "] =" << ((*this).matrix)[row][col] << endl;
}
The operation ((*this).matrix)[row][col]
should return an int
.
I have no idea how to build the operator [][]
.
Alternatively, I could concatenate a couple of calls to the operator []
, but I didn't succeed in it, because the first call to that operaror will return int*
and the second one will return int
, and it compel to build another operator, and I dont want to do that.
What can I do?
Thank you,
Simply, such an operator does not exist, so you can not overload it.
A possible solution is to define two classes: the Matrix and the Row.
You can define the operator[]
of a Matrix
so that it returns a Row
, then define the same operator for the Row
so that it returns an actual value (int
or whatever you want, your Matrix
could be also a template).
This way, the statement myMatrix[row][col]
will be legal and meaningful.
The same can be done in order to assign a new Row
to a Matrix
or to change a value in a Row
.
* EDIT *
As suggested in the comments, also you should take in consideration to use operator()
instead of operator[]
for such a case.
This way, there wouldn't be anymore the need for a Row
class too.
You can define your own operator []
for the class. A straightforward approach can look the following way
#include <iostream>
#include <iomanip>
struct A
{
enum { Rows = 3, Cols = 4 };
int matrix[Rows][Cols];
int ( & operator []( size_t i ) )[Cols]
{
return matrix[i];
}
};
int main()
{
A a;
for ( size_t i = 0; i < a.Rows; i++ )
{
for ( size_t j = 0; j < a.Cols; j++ ) a[i][j] = a.Cols * i + j;
}
for ( size_t i = 0; i < a.Rows; i++ )
{
for ( size_t j = 0; j < a.Cols; j++ ) std::cout << std::setw( 2 ) << a[i][j] << ' ';
std::cout << std::endl;
}
}
The program output is
0 1 2 3
4 5 6 7
8 9 10 11
I have no idea how to build the operator [][]
.
Sometimes it is fine to use a different operator, namely ()
:
int& Matrix::operator () (int x, int y)
{
return matrix[x][y];
}
const int& Matrix::operator () (int x, int y) const
{
return matrix[x][y];
}
int diagonal (const Matrix& m, int x)
{
return m (x, x); // Usage.
}
Advantage:
No need to use "intermediate" class like Row
or Column
.
Better control than with Row& Matrix operator (int);
where someone could use the Row
reference to drop in a row of, say, illegal length. If Matrix
should represent a rectangular thing (image, matrix in Algebra) that's a potential source of error.
Might be less tedious in higher dimensions, because operator[]
needs classes for all lower dimensions.
Disadvantage:
Uncommon, different syntax.
No more easy replacement of complete rows / columns, if that's desired. However, replacing columns is not easy, anyway, provided you used rows to model (and vice versa).
In either case, there are pros and cons if the number of dimensions are not known at runtime.
#include<iostream>
using namespace std;
class matrix
{
int **arr;
int r;
int c;
public:
/* another code*/
class row
{
matrix &_a;
int _i;
public:
row(matrix &a,int i) : _a(a),_i(i){}
int operator[](int j) {return _a.arr[_i][j];}
};
row operator[](int i)
{
return row(*this,i);
}
};
This could help you