Hi Frenz!
Please go through the following program made for matrices:-
template <class T>
class Matrix
{
private:
int dim1;int dim2;
T **mtx;
public:
Matrix(int a)
{
dim1=dim2=a;
mtx=new T*[dim1];
for(int i=0;i<dim1;i++)
mtx[i]=new T[dim2];
}
~Matrix()
{
delete []mtx;
}
Matrix<T> operator +(Matrix<T> M)
{
Matrix C(dim1,dim2);
for(int i=0;i<dim1;i++)
for(int j=0;j<dim2;j++)
C.mtx[i][j]=mtx[i][j]+M.mtx[i][j];
return C;
}
};
Problem with this program is that it creates a temporary matrix C in the function operator+ . This Matrix C calls the destructor before its value can be returned to the matrix, i want to store the result into. Because of this, the program stores random values when matrices are added. I can not remove the destructor because I must release the memory alloted to the temporary matrix C else the total memory is exhausted. I also tried to release the memory in another function but I dont know how & where to call this function. Can somebody please suggest some solution to my problem? :sad: