this is my code
void SMatrix::pow(int power, SMatrix & result)
{
if (this->rowSize != this->colSize || this->rowSize != result.rowSize || this->colSize != result.colSize || power <= 0)
{
delete & result;
result = new SMatrix (result.rowSize, result.colSize);
}
}
Im trying to delete this result in this case, and send it as new SMatrix
. How can i do it? (reuslt = newM
..... ask me for SMatrix *
, but it doesn't work with &).
In the main i can build it like that: SMatrix * s = new SMatrix(4, 4); or SMatrix s(4, 4); (pointer or not).
This code is just "doing it wrong".
If you have a reference argument, then the implied effect is that the ownership of any pointer to it belongs to the caller.
void SMatrix::pow(int power, SMatrix & result)
{
if (this->rowSize != this->colSize || this->rowSize != result.rowSize || this->colSize != result.colSize || power <= 0)
{
delete & result;
result = new SMatrix (result.rowSize, result.colSize);
}
}
If your SMatrix
doesn't have a proper operator=
, then you should have one. In other words, the correct thing should happen if you do:
if (rowSize != colSize || rowSize != result.rowSize || colSize != result.colSize || power <= 0)
{
result = SMatrix (result.rowSize, result.colSize);
}
(Note that I removed both the delete
line and the new
operator)
If this, for some reason, won't work correctly, then you need to fix that, not rely on how the orignal data was allocated.