首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包含向量的类的C++移动构造函数

包含向量的类的C++移动构造函数
EN

Stack Overflow用户
提问于 2016-03-07 01:56:57
回答 2查看 829关注 0票数 2

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

代码语言:javascript
复制
class A
{

    std::vector<double> m;

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

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

我应该这样做吗?

代码语言:javascript
复制
A(A&& other)
    : m{std::move(other.m)}
{
}
EN

回答 2

Stack Overflow用户

发布于 2016-03-07 02:00:27

第二个代码片段是移动other.m的正确方法,因为它是一个左值,需要转换为r-value-reference,以便std::vector move构造函数生效。

即使在这个非常具体的示例中,只需简单地编写

代码语言:javascript
复制
A(A&& rhs) = default;

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

附注:您可能还打算将构造函数设置为公共的。

票数 1
EN

Stack Overflow用户

发布于 2020-12-13 15:10:43

代码语言:javascript
复制
/******************************************************************************
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35830393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档