共用方式為


How to: 撰寫移動建構函式

本主題說明如何撰寫移動建構函式和 C++ 類別移設定運算子。 移動的建構函式可讓您實作移動語意,可以大幅改進應用程式的效能。 如需有關移動語意的詳細資訊,請參閱右值參考的宣告子: & &

本主題會根據下列的 C++ 類別, MemoryBlock,它會管理記憶體緩衝區。

// MemoryBlock.h
#pragma once
#include <iostream>
#include <algorithm>

class MemoryBlock
{
public:

   // Simple constructor that initializes the resource.
   explicit MemoryBlock(size_t length)
      : _length(length)
      , _data(new int[length])
   {
      std::cout << "In MemoryBlock(size_t). length = "
                << _length << "." << std::endl;
   }

   // Destructor.
   ~MemoryBlock()
   {
      std::cout << "In ~MemoryBlock(). length = "
                << _length << ".";
      
      if (_data != NULL)
      {
         std::cout << " Deleting resource.";
         // Delete the resource.
         delete[] _data;
      }

      std::cout << std::endl;
   }

   // Copy constructor.
   MemoryBlock(const MemoryBlock& other)
      : _length(other._length)
      , _data(new int[other._length])
   {
      std::cout << "In MemoryBlock(const MemoryBlock&). length = " 
                << other._length << ". Copying resource." << std::endl;

      std::copy(other._data, other._data + _length, _data);
   }

   // Copy assignment operator.
   MemoryBlock& operator=(const MemoryBlock& other)
   {
      std::cout << "In operator=(const MemoryBlock&). length = " 
                << other._length << ". Copying resource." << std::endl;

      if (this != &other)
      {
         // Free the existing resource.
         delete[] _data;

         _length = other._length;
         _data = new int[_length];
         std::copy(other._data, other._data + _length, _data);
      }
      return *this;
   }

   // Retrieves the length of the data resource.
   size_t Length() const
   {
      return _length;
   }

private:
   size_t _length; // The length of the resource.
   int* _data; // The resource.
};

下列程序說明如何撰寫移動建構函式和移動設定運算子為使本範例 C++ 類別。

若要建立 C++ 類別的建構函式移動

  1. 定義空白建構函式方法可接受的類別型別未右值的參考做為參數,如下列範例所示:

    MemoryBlock(MemoryBlock&& other)
       : _data(NULL)
       , _length(0)
    {
    }
    
  2. 移動的建構函式,從來源物件正在用來建構的物件指派的類別資料成員:

    _data = other._data;
    _length = other._length;
    
  3. 將來源物件的資料成員指定為預設值。 這可避免多次釋放資源 (例如記憶體) 解構函式:

    other._data = NULL;
    other._length = 0;
    

若要建立 C++ 類別的移動工作分派運算子

  1. 定義空的指派運算子,以取得做為參數的類別型別未右值的參考,並傳回參考類別型別,如下列範例所示:

    MemoryBlock& operator=(MemoryBlock&& other)
    {
    }
    
  2. 在移動的指派運算子,加入條件陳述式,會執行任何作業,如果您嘗試將物件指派給自己。

    if (this != &other)
    {
    }
    
  3. 條件式陳述式中,在釋放任何資源 (例如記憶體) 被指派至 object。

    下列範例會釋放_data被指派至 object 的成員:

    // Free the existing resource.
    delete[] _data;
    

    請依照下列步驟 2 和 3 在第一個程序,從來源物件的資料成員轉移至正在建構的物件:

    // 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;
    
  4. 將參照傳回至目前的物件,如下列範例所示:

    return *this;
    

範例

下列範例顯示完整的則移建構函式,而移設定運算子,如MemoryBlock類別:

// Move constructor.
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;
}

// Move assignment operator.
MemoryBlock& operator=(MemoryBlock&& other)
{
   std::cout << "In operator=(MemoryBlock&&). length = " 
             << other._length << "." << std::endl;

   if (this != &other)
   {
      // Free the existing resource.
      delete[] _data;

      // 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;
   }
   return *this;
}

下列範例會示範如何移動語意可以改善應用程式的效能。 此範例將兩個項目加入至向量物件,並且再插入兩個現有元素之間的新項目。 在Visual C++ 2010、 vector類別使用移動移動向量,而不是將它們複製的項目,有效率地執行插入作業的語意。

// rvalue-references-move-semantics.cpp
// compile with: /EHsc
#include "MemoryBlock.h"
#include <vector>

using namespace std;

int main()
{
   // Create a vector object and add a few elements to it.
   vector<MemoryBlock> v;
   v.push_back(MemoryBlock(25));
   v.push_back(MemoryBlock(75));

   // Insert a new element into the second position of the vector.
   v.insert(v.begin() + 1, MemoryBlock(50));
}

這個範例會產生下列輸出:

In MemoryBlock(size_t). length = 25.
In MemoryBlock(MemoryBlock&&). length = 25. Moving resource.
In ~MemoryBlock(). length = 0.
In MemoryBlock(size_t). length = 75.
In MemoryBlock(MemoryBlock&&). length = 25. Moving resource.
In ~MemoryBlock(). length = 0.
In MemoryBlock(MemoryBlock&&). length = 75. Moving resource.
In ~MemoryBlock(). length = 0.
In MemoryBlock(size_t). length = 50.
In MemoryBlock(MemoryBlock&&). length = 50. Moving resource.
In MemoryBlock(MemoryBlock&&). length = 50. Moving resource.
In operator=(MemoryBlock&&). length = 75.
In operator=(MemoryBlock&&). length = 50.
In ~MemoryBlock(). length = 0.
In ~MemoryBlock(). length = 0.
In ~MemoryBlock(). length = 25. Deleting resource.
In ~MemoryBlock(). length = 50. Deleting resource.
In ~MemoryBlock(). length = 75. Deleting resource.

之前Visual C++ 2010,此範例會產生下列輸出:

In MemoryBlock(size_t). length = 25.
In MemoryBlock(const MemoryBlock&). length = 25. Copying resource.
In ~MemoryBlock(). length = 25. Deleting resource.
In MemoryBlock(size_t). length = 75.
In MemoryBlock(const MemoryBlock&). length = 25. Copying resource.
In ~MemoryBlock(). length = 25. Deleting resource.
In MemoryBlock(const MemoryBlock&). length = 75. Copying resource.
In ~MemoryBlock(). length = 75. Deleting resource.
In MemoryBlock(size_t). length = 50.
In MemoryBlock(const MemoryBlock&). length = 50. Copying resource.
In MemoryBlock(const MemoryBlock&). length = 50. Copying resource.
In operator=(const MemoryBlock&). length = 75. Copying resource.
In operator=(const MemoryBlock&). length = 50. Copying resource.
In ~MemoryBlock(). length = 50. Deleting resource.
In ~MemoryBlock(). length = 50. Deleting resource.
In ~MemoryBlock(). length = 25. Deleting resource.
In ~MemoryBlock(). length = 50. Deleting resource.
In ~MemoryBlock(). length = 75. Deleting resource.

這個範例使用移動語意的版本是較有效率的 mdac 版本,因為它會執行較少的複本、 記憶體配置和記憶體解除配置作業不會使用移動語意。

穩固程式設計

若要避免資源遺漏,一律在移動的工作分派運算子釋放資源 (例如記憶體、 檔案控制代碼和通訊端)。

若要避免資源的解構時,無法復原,適當地處理 self-assignment 中移動的工作分派運算子。

如果您為您的類別,提供移動建構函式,並移動指派運算子,就可以排除多餘的程式碼撰寫的移動建構函式呼叫移動的工作分派運算子。 下列範例將示範移動建構函式呼叫移動指派運算子的修訂的版本:

// Move constructor.
MemoryBlock(MemoryBlock&& other)
   : _data(NULL)
   , _length(0)
{
   *this = std::move(other);
}

Std::move 函式會保留右值屬性的other參數。

請參閱

參考

右值參考的宣告子: & &

其他資源

<utility> move