我正在尝试了解 C++ 的新功能,即移动构造函数和赋值X::operator=(X&&)
,我发现了有趣的示例 ,但唯一我什至不理解但更不同意的是移动 ctor 和赋值运算符中的一行(在下面的代码中标记):
MemoryBlock(MemoryBlock&& other)
: _data(NULL)
, _length(0)
{
std::cout << "In MemoryBlock(MemoryBlock&&). length = "
<< other._length << ". Moving resource." << std::endl;
// Copy the data pointer and its length from the
// source object.
_data = other._data;
_length = other._length;
// Release the data pointer from the source object so that
// the destructor does not free the memory multiple times.
other._data = NULL;
other._length = 0;//WHY WOULD I EVEN BOTHER TO SET IT TO ZERO? IT DOESN'T MATTER IF IT'S ZERO OR ANYTHING ELSE IT IS JUST A VALUE.
}
所以我的问题是:我必须将 lenght_ 的值设置为零还是可以保持不变?不会有任何内存泄漏,也不会少一个表情。