-1

I'm attempting to create a matrix class where the output of the overloaded plus operator is a new matrix that contains the sum of the two matrices passed in. However, I would like to preallocate the result in order to improve the time efficiency of this operator. Everywhere that I've searched so far shows that the overloaded operator+ instantiates a new object that it returns by value, which is then copied to whatever variable it is being assigned to. Is there anyway to avoid this?

Drew
  • 101
  • 1
  • 3
  • You have code which works and which you want to modify for the described purpose. Show it as an [mcve], please. – Yunnosch May 21 '18 at 19:23

1 Answers1

0

Modern c++ compilers will omit the copy of the returned value, if they don't then you just need to make sure your class is movable and that will then also avoid they copy.

Put your code into http://godbolt.org to see the generated assembly.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60