我重载了一个乘法运算符:
Matrix& Matrix::operator*(double value)
{
Matrix temp2(this->Rows,this->Cols);
temp2=*this;
for(int i=0;i<Rows;i++)
{
for(int j=0;j<Cols;j++)
{
*(temp2.matrix+i*temp2.Cols+j)*=(double)value;
}
}
return temp2;
}
编绎没错,运行出错。
然后我改了:
Matrix& Matrix::operator*(double value)
{
static Matrix temp2(this->Rows,this->Cols);
temp2=*this;
for(int i=0;i<Rows;i++)
{
for(int j=0;j<Cols;j++)
{
*(temp2.matrix+i*temp2.Cols+j)*=(double)value;
}
}
return temp2;
}
就可以了,这是那个叫什么生存域的问题吗?但:
int f()
{
int a=9;
return a;
}为什么没问题了。
还有为什么在这里可以调用temp2私有成员了(double *matrix;int Rows;int Cols是Matrix的私有成员)。