相关疑难解决方法(0)

什么是C++中的复制/移动构造函数选择规则?什么时候发生移动复制后备?

第一个例子:

#include <iostream>
#include <memory>
using namespace std;

struct A {
    unique_ptr<int> ref;
    A(const A&) = delete;
    A(A&&) = default;
    A(const int i) : ref(new int(i)) { }
    ~A() = default;
};

int main()
{
    A a[2] = { 0, 1 };
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

它完美地运作.所以这里使用了MOVE构造函数.

让我们删除移动构造函数并添加一个副本:

#include <iostream>
#include <memory>
using namespace std;

struct A {
    unique_ptr<int> ref;
    A(const A&a) 
        : ref( a.ref.get() ? new int(*a.ref) : nullptr )
    {  }
    A(A&&) = delete;
    A(const int i) : ref(new …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor move-constructor c++11

15
推荐指数
2
解决办法
1008
查看次数

标签 统计

c++ ×1

c++11 ×1

copy-constructor ×1

move-constructor ×1