Just as I was about to pat myself on the back for finally understaning pointers, I encountered this problems that reminded me once again of my ignorance.
So, consider the operator overload below.
MatrixType MatrixType::operator +(MatrixType &rightMatrix)
{
MatrixType* _result;
int _cols = this->getCols();
int _rows = this->getRows();
if ((rightMatrix.getCols() != _cols) || (rightMatrix.getRows() != _rows))
{
throw ("Cannot add matrices of different sizes");
}
_result = new MatrixType(_cols, _rows);
// Code here that fills _result with elements
// I have verified that _result does indeed get
// correctly filled.
return *_result;
}
Now consider how I use it. When I call *_matrix3 = *_matrix1 + *_matrix2 (below),
the code compiles and runs OK but _matrix3 ends up with undefined elements.
Its size is OK, but the elemets are not what was returned from the operator
overload+ method. I've tried some variations on this, using *, not using *, but I
get compile errors. I'm at a loss of how to set this up. Any ideas
void MatrixTester::BeginTest()
{
MatrixType* _matrix1;
MatrixType* _matrix2;
MatrixType* _matrix3;
// Some code here to fill _matrix1 and _matrix2
_matrix3 = new MatrixType(_matrix1->getCols(), _matrix1->getRows());
*_matrix3 = *_matrix2 + *_matrix1;
}
Thanks,