B
bisuvious
hello C++ gurus,
I am having a problem and want help fom you.
Here is my problem-------------
I am designing a class for matrix which dynamically creates a 2d array
to hold data and have its binary overloaded operators like +,-,* etc.
Here is my codes:
class matrix
{
public:
matrix(int r, int c);
~matrix();
matrix& operator = (const matrix& src); //assignment operator
matrix operator + (const matrix& x); //addition operator
private:
int row;
int column;
int** data;
};
matrix::matrix(int r, int c)
{
//assign row and columns
row = r;
column = c;
//now allocate memory for data
data = new int* [row];
for(int i = 0; i < column; ++i)
data = new int[column];
}
matrix::~matrix()
{
//we need to free the memory allocated earlier
if( data != NULL)
{
for(int i = 0; i < row; ++i)
delete[] data;
delete []data;
}
}
matrix matrix:
perator + (const matrix& x)
{
matrix temp(row,column);
//add 2 matrices and store the result into temp
return temp;
}
If I want to add 2 matrices as
matrix a(2,2), b(2,2), c(2,2);
//initialise a,b with data
c = a + b;
then the following sequence of function calls happens:
1. firstly invokes matrix:
perator + (matrix& x).
2.Just after "return temp;" statement of +operator(), destructor for
temp is called which deallocates the memory for temp.
3.Then assignment operator for "c = a + b;" statement is called which
tries to assign data from "temp" to "c". But here it fails due to
unavailability of "temp" memory.
Interestingly if I do not allow to delete memory in destructor then it
works fine. Also if I use stack memory for the 2d array(do not allocate
memory dynamically, that means say int data[3][3] ) then also fine.
I do not want to solve this problem by using a 2nd parameter of type
matrix and passing "c" in this parameter.
any suggestion in this regards will be most welcome.
thanks
bisuvius
I am having a problem and want help fom you.
Here is my problem-------------
I am designing a class for matrix which dynamically creates a 2d array
to hold data and have its binary overloaded operators like +,-,* etc.
Here is my codes:
class matrix
{
public:
matrix(int r, int c);
~matrix();
matrix& operator = (const matrix& src); //assignment operator
matrix operator + (const matrix& x); //addition operator
private:
int row;
int column;
int** data;
};
matrix::matrix(int r, int c)
{
//assign row and columns
row = r;
column = c;
//now allocate memory for data
data = new int* [row];
for(int i = 0; i < column; ++i)
data = new int[column];
}
matrix::~matrix()
{
//we need to free the memory allocated earlier
if( data != NULL)
{
for(int i = 0; i < row; ++i)
delete[] data;
delete []data;
}
}
matrix matrix:
{
matrix temp(row,column);
//add 2 matrices and store the result into temp
return temp;
}
If I want to add 2 matrices as
matrix a(2,2), b(2,2), c(2,2);
//initialise a,b with data
c = a + b;
then the following sequence of function calls happens:
1. firstly invokes matrix:
2.Just after "return temp;" statement of +operator(), destructor for
temp is called which deallocates the memory for temp.
3.Then assignment operator for "c = a + b;" statement is called which
tries to assign data from "temp" to "c". But here it fails due to
unavailability of "temp" memory.
Interestingly if I do not allow to delete memory in destructor then it
works fine. Also if I use stack memory for the 2d array(do not allocate
memory dynamically, that means say int data[3][3] ) then also fine.
I do not want to solve this problem by using a 2nd parameter of type
matrix and passing "c" in this parameter.
any suggestion in this regards will be most welcome.
thanks
bisuvius