Почему в операторе присваивания мы возвращаем *this
, а не просто this
?
Matrix & Matrix::operator =(const Matrix& m2) {
if (this != &m2) {
(Matrix(m2)).swap(*this);
}
return *this;
}
Matrix Matrix::operator +(const Matrix &m2) {
if (m2.columns != this->columns || m2.rows != this->rows) {
std::cout << "Error" << std::endl;
exit(1);
}
Matrix temp(this->rows, this->columns);
for (int i = 0; i < this->rows; i++) {
for (int j = 0; j < this->columns; j++) {
temp[i][j] = m2.matrix[i][j] + this->matrix[i][j];
}
}
return temp;
}
Matrix&
- это*this
, аMatrix*
- этоthis
:) – int3 5 июн '17 в 16:30