I'm implementing a matrix class with some arithmetic. This is what I had as function for addition:
...
Matrix Matrix::operator+(const Matrix& other) const
{
Matrix out = *this;
out += scalar;
return out;
}
void Matrix::operator+=(const Matrix& other)
{
// Actually adding the numbers.
}
Just recently I found out that besides the copy constructor, I should also define a move constructor to avoid unnecessary copying of my matrix class. So I did that. And here's where it goes wrong. In the first line of my + operator, it will now move instead of copy and that causes off course a memory error.
So I revised to the following:
...
MatrixNT::MatrixNT(const MatrixNT&& other)
{
m_logger = LogWrapper::getLogger("MatrixNT");
m_rows = other.m_rows;
m_cols = other.m_cols;
m_data = other.m_data;
}
...
Matrix Matrix::operator+(const Matrix& other) const
{
Matrix out(*this);
out += scalar;
return out;
}
Is this the correct way of calling the copy constructor? It seems a bit odd, using the deference operator again. I can't find any examples on it.