I am using operator to add two matrices stored inside matrix objects. I instantiate and initialize two matrices. Then I call operator to add them. But before adding the two matrices: m1 and m2 , their destructors gets called. When I start adding the matrices since m2 matrix array has been deleted, it crashes. How can I force the destrucotr to get called on the matrices after I add the two matrices together.
// calling matrix initialization and addition
m1.initMatrix();
m1.print();
m2.initMatrix();
m2.print();
rslt = m1-m2;
//operator
matrix matrix::operator +(matrix m1)
{
matrix rslt = getResultMatrix(rows, cols, 0);
for(int n=0; n<iterations; n++)
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
rslt.m[i][j] = this->m[i][j]+m1.m[i][j];
}
}
}
return rslt;
}
// destructor
matrix::~matrix()
{
delete[] this->m;
}