Pass by reference, source modified in the middle of the operation

I am working on a program that solves a system of equations in matrix form by eliminating Gauss. However, I came across an interesting problem: if my arithmetic operators follow a link, normalizing strings gives incorrect results.

In my implementation, the matrix consists of several vectors, so the operations in the line are just vector arithmetic. Here are the related functions:

Vector:

T& operator[] (const int i);
const T& operator[] (const int i) const;
Vector<T>& operator/=(const T& rhs);

template<class T>
Vector<T>& Vector<T>::operator/=(const T& rhs)
{
  if (rhs == 0)
  {
    throw DivideByZeroException();
  }
  for (int i = 0; i < _size; ++i)
  {
    _data[i] /= rhs;
  }
  return *this;
}

The matrix:

Vector<T>& operator[] (const int i);
const Vector<T>& operator[] (const int i) const;

(Thus, a single [] is used to access a string, and double [] [] is used to access an element.)

Now here is the line that causes the problem:

mat[i] /= mat[i][i];

, - mat[i][i], , operator/= pass by reference.

. ( ) , ? , , , , ?

+4
1

, , , /=, . , () . , i = i++ + ++i; .

, - , , , , .

+2

Source: https://habr.com/ru/post/1533835/


All Articles