Operator + for matrices in C++
- by cibercitizen1
I suppose the naive implementation of a + operator for matrices (2D for instance)
in C++ would be:
class Matrix {
Matrix operator+ (Matrix other) const {
Matrix result;
// fill result with *this.data plus other.data
return result;
}
}
so we could use it like
Matrix a;
Matrix b;
Matrix c;
c = a + b;
Right?
But if matrices are big this is not efficient as we are doing one not-necessary copy (return result).
Therefore, If we wan't to be efficient we have to forget the clean call:
c = a + b;
Right?
What would you suggest / prefer ?
Thanks.