简体   繁体   English

从函数返回带有动态字段的对象c ++

[英]return from function, object with dynamic fields c++

I have a class named Matrix that contains one field that is stored dynamic. 我有一个名为Matrix的类,其中包含一个动态存储的字段。 And a method named Multiply() that has to return the result of multiplying 2 Matrix. 还有一个名为Multiply()的方法,该方法必须返回2矩阵相乘的结果。 Problem is that I defined a destructor and when I return, variable which stores the resultant Matrix get some random values, I guess it happens because new variable have the same address as temporary Matrix. 问题是我定义了一个析构函数,当我返回时,存储结果矩阵的变量会得到一些随机值,我猜是因为新变量的地址与临时矩阵相同。 How can I return it correctly? 如何正确退货?

class Matrix{
    double **val;
    int rows,cols,errorCode;
public:
    Matrix();
    Matrix(int);
    Matrix(int, int);
    ~Matrix();
    void Print();
    void Read();
    void Realoc(int, int );
    void Assign(int,int,double);
    Matrix Multiply(Matrix&);
    void Multiply(double);
};

Matrix Matrix::Multiply(Matrix &a){
    if(cols != a.rows){
        Matrix b;
        b.errorCode=112; //That means matrices are not compatible;
        cout<<"WARNING! Error "<<errorCode<<" has occurred. "<<endl;
        return b;
    }
    else{
            //Making a new matrix where we save computed values;
        Matrix b;
        b.Realoc(rows,a.cols);


            //Computing values;
        double  p;
        for(int i=0;i<rows;i++){
            for(int j=0;j<a.cols;j++){
                p=0;
                for(int k=0;k<cols;k++){p += val[i][k]*a.val[k][j];}
                b.Assign(i+1,j+1,p);
            }
        }
        return b;
    }
}

int main(){



Matrix a,b(2,2);
b.Assign(1,1,0);
b.Assign(1,2,3);
b.Assign(2,1,5);
b.Assign(2,2,5);
b.Print();

a.Read();
cout<<endl;
cout<<"'a' multiplied by 'b' is: "<<endl;
Matrix m;
m = a.Multiply(b);
m.Print();
cout<<endl;
return 0;
}

Some ideas? 有什么想法吗?

PS I made copy constructor but it do not do any good result. PS我做了副本构造函数,但效果不佳。

Here is a copy constructor I made. 这是我制作的副本构造函数。

Matrix::Matrix(Matrix &a){
    rows = a.rows;
    cols = a.cols;
    errorCode = 0;
    val = new double*[rows];
    for(int i = 0;i<rows;i++){
        val[i] = new double[cols];
    }
    for(int i=0;i<rows;i++){
        for(int j=0;j<cols;j++){
            val[i][j] = a.val[i][j];
        }
    }
}

And destructor: 和析构函数:

Matrix::~Matrix(){
    for(int i=0;i<rows;i++){
        delete[] val[i];
    }
    delete[] val;
}

This: 这个:

m = a.Multiply(b);

Is calling the assignment operator and not the copy constructor as m has already been default constructed. 正在调用赋值运算符,而不是复制构造函数,因为m已被默认构造。 The default assignment operator is not going to be good enough as you are dealing with dynamic memory allocation. 在处理动态内存分配时,默认的赋值运算符将不够好。 You will need to implement you own assignment operator. 您将需要实现自己的赋值运算符。 I suggest you take a look at What is The Rule of Three? 我建议您看看什么是“三法则”?

I would also suggest you just use a 2d std::vector like std::vector<std::vector<double>> as with it the defaults provided by the compiler would work for you. 我还建议您只使用2d std :: vector,std::vector<std::vector<double>>因为它与编译器提供的默认值一样适合您。 As you said it is a requirement to use a double** you will need to implement the constructors and assignment operator yourself. 如您所说,使用double**是一个要求,您将需要自己实现构造函数和赋值运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM