小编ker*_*los的帖子

了解复制/移动构造函数和运算符之间的推理

我试图通过一个简单的自制示例来掌握右值引用并移动语义,但我无法理解特定部分。我创建了以下课程:

class A {
public:
    A(int a) {
        cout << "Def constructor" << endl;
    }

    A(const A& var) {
        cout << "Copy constructor" << endl;
    }

    A(A&& var) {
        cout << "Move constructor" << endl;
    }

    A& operator=(const A& var) {
        cout << "Copy Assignment" << endl;
        return *this;
    }

    A& operator=(A&& var) {
        cout << "Move Assignment" << endl;
        return *this;
    }
};
Run Code Online (Sandbox Code Playgroud)

我尝试了以下实验,看看我是否可以预测构造函数/运算符将如何被调用:

  1. A a1(1) - 将调用默认构造函数。 预料到的
  2. A a2 = a1 - 将调用复制构造函数。预料到的
  3. a1 …

c++ rvalue-reference move-semantics c++11

6
推荐指数
1
解决办法
1260
查看次数

标签 统计

c++ ×1

c++11 ×1

move-semantics ×1

rvalue-reference ×1