template<int _size>
Matrix<_size> Matrix<_size>::operator + (const Matrix< _size> &rhs) const
{
Matrix<_size> m;
for (int i = 0; i < (_size*_size); ++i)
m.elements[i] = elements[i] + rhs.elements[i];
return m;
}
The function above inits a new object in a function and returns it. Doesnt this mean that as soon as the program has executed this method,the memory used to store Matrix m, can be garbage collected?
I thought it was bad practice to create pointers/objects in a method and return them in c++?
You can return a pointer or reference from a function, just make sure it's not a reference/pointer to a local object. Since local objects go out of scope when the function call ends, a pointer/reference to that object would be left dangling; not a good thing, especially for references.