I am having trouble understanding c++ move semantics. Suppose I want to return a large object from a method. In order to make this operation efficient, I use a move constructor for the large object class. I call a method to obtain a large object and pass it to another method that will process it.
processLargeObject(getLargeObject());
I write a method getLargeObject()
where I create a large object. If I create the object without the new operator, I am given to understand that the storage is allocated on the stack. In a case where I know the size of the object in advance and I represent it in an array, this large object can be defined as double foo[1000000000];
. Now inside the move constructor, if I say
foo = other.foo;
then the moved object will still reside on the stack and can be overwritten as the stack expands.
So this must not be how a move constructor is used. Should a move constructor be used only for moving objects that reside on the heap?