为什么编译器在此示例中选择了复制构造函数到位赋值运算符?

时间:2017-02-11 17:01:44

标签: c++

我对以下步骤#2中与复制构造函数相关的看似简单的问题感到困惑,请举例说明foo&使用operator =(foo&& other)。

 foo foo1;
 foo foo2=foo1; // why it uses foo(const foo &other); constructor instead of foo& operator=(const foo &other) 
 foo2 = foo1; // ok, it uses foo& operator=(const foo &other) 

 A obj6(move(obj5)); // ok it uses foo(foo &&other)

1 个答案:

答案 0 :(得分:2)

foo foo2=foo1; // why it uses foo(const foo &other); constructor instead of foo& operator=(const foo &other) 

因为这是复制初始化的语法。复制初始化使用复制(或移动)构造函数,而不是复制(或移动)赋值运算符。