Operator overload + (Plus) without changing first term

21 Views Asked by At

Currently, the code uses a member function operator + overload like this

Matrix operator + (const Matrix& B){
    int l = (int)B.a.size();
    int b = (int)B.a[0].size();
    for (int i = 0; i < l; i++){
        for (int j = 0; j < b; j++){
            this->a[i][j] = this->a[i][j] + B.a[i][j];
        }
    }
    return *this;
    }

Calling the + operator overload looks like

Matrix x;
Matrix y;
Matrix result;
result = x + y;

The code alters x also because we use this->a[i][j] = this->a[i][j] + b.a[i][j] the x gets updated to become result. How do I keep x constant and add the sum of x,y and put it into result?

0

There are 0 best solutions below