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.
The data matrix is defined like
int** matrix; matrix = new int*[row];
if (matrix == NULL)
{
cout << "Allocation memory - Failed";
}
for (int i = 0; i < row; i++)//Allocation memory
{
matrix[i] = new int[col];
if (matrix[i] == NULL)
{
cout << "Allocation memory - Failed";
return;
}
}
What can I do? Thank you,
(((*this).matrix)[row])[col]
so the first operator will return int* and the second int. – Supralapsarianint** matrix; matrix = new int*[row]; if (matrix == NULL) { cout << "Allocation memory - Failed"; } for (int i = 0; i < row; i++)//Allocation memory { matrix[i] = new int[col]; if (matrix[i] == NULL) { cout << "Allocation memory - Failed"; return; } }
– Supralapsarian