-1

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;
Samu
  • 101
  • 1
  • 7

1 Answers1

0

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.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • So what is the use of const in that specific operator? I saw it on some slides on a google search and I am wondering why they chose to use const – Samu Oct 30 '16 at 17:45
  • it returns a constant object, but it is immediately passed to the assignment operator, which takes the left-side object as non-const and changes it. – Jean-François Fabre Oct 30 '16 at 17:49