【问题标题】:C++ Move constructor for class containing vector包含向量的类的 C++ 移动构造函数
【发布时间】:2016-03-06 17:56:56
【问题描述】:

我用以下方式为一个类编写了一个移动构造函数:

class A
{

    std::vector<double> m;

    A(A&& other)
        : m{other.m}
    {
    }
}

这是将other.m 移动到m 的正确方法吗?

我应该这样做吗?

A(A&& other)
    : m{std::move(other.m)}
{
}

或者我应该完全做其他事情?

【问题讨论】:

  • 您应该将其保留为默认值。零规则。

标签: c++11 move-constructor


【解决方案1】:

第二个 sn-p 是移动other.m 的正确方法,因为它是一个左值,需要将std::vector 移动构造函数转换为右值引用。

尽管在这个非常具体的例子中,简单地写就足够了

A(A&& rhs) = default;

编译器将生成一个构造函数,将rhs 的每个成员移动到*this 的相应成员。

附言您可能还打算公开构造函数。

【讨论】:

  • 我更喜欢手动编写它们以进行练习,通常我的类中会有非平凡的移动/复制构造函数/赋值。我必须在移动赋值运算符中使用 std::move 吗?目前,我只是在使用 std::swap?也许我应该把这个作为一个单独的问题来问?
  • 如果你想移动它们,你应该在有名字的变量上使用std::move。它是什么功能并不重要。
【解决方案2】:
/******************************************************************************
Below program demonstrates how to use move constructor and move assignment operator

*******************************************************************************/

#include <iostream>
#include <algorithm>
#include <vector>

class MemoryBlock
{
public:

 MemoryBlock()
   {
       this->id++;
      std::cout << "Default Constructor"<<std::endl;
   }

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

   // Destructor.
   ~MemoryBlock()
   {
       this->id--;
      std::cout << "Destructor In ~MemoryBlock(). length = and id ="
                << _length << "."<<id;

      if (_data != nullptr)
      {
         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])
   {
       this->id++;
      std::cout << " Copy Constructor MemoryBlock(const MemoryBlock&). length = and id ="
                << other._length << "." <<id<<"Copying resource." << std::endl;

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

   // Copy assignment operator.
   MemoryBlock& operator=(const MemoryBlock& other)
   {
      std::cout << "Assignment operator 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;
   }
   
  //Move copy constructor 
  MemoryBlock(MemoryBlock&& other) noexcept
   : _data(nullptr)
   , _length(0)
{
   std::cout << "Move Constructor 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 = nullptr;
   other._length = 0;
}

// Move assignment operator.
MemoryBlock& operator=(MemoryBlock&& other) noexcept
{
   std::cout << "Move assignment operator 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 = nullptr;
      other._length = 0;
   }
   return *this;
}

private:
   size_t _length; // The length of the resource.
   int* _data; // The resource.
   static int id;
};
int MemoryBlock::id=0;
int main()
{
    std::vector<MemoryBlock> v1;
    MemoryBlock m1(100);
    MemoryBlock m2(100);
    MemoryBlock m3(100);
    v1.push_back(m1);
    v1.push_back(m2);
    v1.push_back(m3);
    return 0;
}

【讨论】:

    猜你喜欢
    • 2013-12-13
    • 2017-09-24
    • 1970-01-01
    • 2017-06-11
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    相关资源
    最近更新 更多