Microsoft示例的此移动构造函数似乎具有冗余代码

Ses*_*i R 7 c++ move-constructor c++11

开发人员的MSDN页面包含以下代码段:

// Move constructor.  
MemoryBlock(MemoryBlock&& other) : _data(nullptr), _length(0)  
{  
   std::cout << "In MemoryBlock(MemoryBlock&&). length = "   
             << other._length << ". Moving resource." << std::endl;  

   // Copy the data pointer and its length from source object.  
   _data = other._data;      // Assginment 1
   _length = other._length;  // Assignment 2

   // Release the data pointer from the source object so that  
   // the destructor does not free the memory multiple times.  
   other._data = nullptr;  
   other._length = 0;  
}
Run Code Online (Sandbox Code Playgroud)

有什么用的_data(nullptr),_length(0)当标记指示作业1作业2过写的价值观_data_length

SJH*_*owe 4

当然应该是

// Move constructor.  
MemoryBlock(MemoryBlock&& other) : _data(other._data), _length(other._length)
{  
   std::cout << "In MemoryBlock(MemoryBlock&&). length = "   
             << other._length << ". Moving resource." << std::endl;  

   // Release the data pointer from the source object so that  
   // the destructor does not free the memory multiple times.  
   other._data = nullptr;  
   other._length = 0;  
}  
Run Code Online (Sandbox Code Playgroud)