3 ms·
> the destructor is still called on the moved-from object Wait, what? If I have a class with a pointer to some allocated memory, then the constructor should a
by AnimalMuppet 4y ago
> the destructor is still called on the moved-from object
Wait, what? If I have a class with a pointer to some allocated memory, then the constructor should allocate the memory, the destructor should free it, copy should allocate an equal-sized amount of memory, and move should just give the destination the pointer (meaning that the destination now owns the allocated block, the original does not). Calling the destructor on the original means that the destination's block of memory is freed (unless you null the origin's pointer as part of the move).
I've never actually written a move that I recall, but this behavior is very surprising to me.
- ninkendo 4y agoYup, that’s why the move constructor for (e.g.) std::string has to call something like `source.buffer = nullptr;`… to prevent the source’s destructor from freeing the storage that was moved. The article covers this point pretty well.
- AnimalMuppet 4y agoTIL.