Samu Samu - 9 months ago 40
C++ Question

Overloading + operator in c++ and the use of const

This function returns a const Matrix A when I plus a Matrix B and C. But then I should not be able to change that const Matrix, for example

A = A + D;
, but I am. Could someone explain to me why that is possible?

const Matrix operator+(const Matrix&) const;

Answer Source

A + D creates a new Matrix instance without changing A but the problem here is that you're assigning it back to A.

The assignment operator changes A, not your addition operator. The const property cannot extend to the full expression.