我对以下步骤#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)
答案 0 :(得分:2)
foo foo2=foo1; // why it uses foo(const foo &other); constructor instead of foo& operator=(const foo &other)
因为这是复制初始化的语法。复制初始化使用复制(或移动)构造函数,而不是复制(或移动)赋值运算符。