C++ 我可以委托移动构造函数吗?

C++ 我可以委托移动构造函数吗?,c++,constructor,move-constructor,C++,Constructor,Move Constructor,我在类Foo中定义了复制构造函数和移动构造函数。在调试中,我在f=CreateFoo(1,1)中进行了一步并将我带到Foo(Foo&&other):Foo(other,0){}。中的下一步将带我到Foo(Foo&other,intm):x(other.x+m),y(other.y-m){},这是一个复制构造函数。我正在委托一个move构造函数,我希望它执行Foo(Foo&&other,int m):x(std::move(other.x)),y(std::move(other.y))。我不明白为

我在类Foo中定义了复制构造函数和移动构造函数。在调试中,我在
f=CreateFoo(1,1)中进行了一步并将我带到
Foo(Foo&&other):Foo(other,0){}
。中的下一步将带我到
Foo(Foo&other,intm):x(other.x+m),y(other.y-m){}
,这是一个复制构造函数。我正在委托一个move构造函数,我希望它执行
Foo(Foo&&other,int m):x(std::move(other.x)),y(std::move(other.y))
。我不明白为什么?谁能给我一些帮助吗?下面是完整的程序

class Foo
{
public:
    int x;
    int y;
public:
    Foo(int i, int j) : x(i), y(j) {}
    Foo(Foo& other) : x(other.x), y(other.y) {}
    Foo(Foo& other, int m) :x(other.x + m), y(other.y - m) {}
    Foo(Foo&& other, int m) : x(std::move(other.x)), y(std::move(other.y))
    {
        x = x + m;
        y = y - m;
    }
    Foo(Foo&& other) : Foo(other, 0) {}
    Foo& operator=(const Foo& other) {
        x = other.x;
        y = other.y;
        return *this;
    }

    Foo& operator=(Foo&& other)
    {
        x = std::move(other.x);
        y = std::move(other.y);
        return *this;
    }
};

Foo CreateFoo(int x, int y)
{
    Foo tmp(x, y);
    return tmp;
}

int main()
{
    Foo f(0, 0);
    f = CreateFoo(1, 1);
    system("pause");
    return 0;
}
Foo(Foo&&other):Foo(other,0){
这里的
other
是一个左值(它有一个名称!)。您需要再次使用
std::move
委派调用,使其成为xvalue:

Foo(Foo&&other):Foo(std::move(other),0){
Foo(Foo&&other):Foo(other,0){
这里的
other
是一个左值(它有一个名称!)。您需要再次使用
std::move
委派调用,使其成为xvalue:

Foo(Foo&&other):Foo(std::move(other),0){
您必须使用
std::move(other)
进行委托。
Foo(Foo&&other)
内部可能存在重复,
other
是一个左值,因为它有一个名称,这就是
Foo(Foo&other,int m)
被委托给的原因。使用
std::move(other)
创建一个右值引用,这样它就可以委托给
Foo(Foo&&other,int m)
而不是:
Foo(Foo&&other):Foo(std::move(other),0){}
重复谈论基本构造函数,但这里同样适用。复制构造函数应该通过
const
引用获取其参数。您必须使用
std::move(other)
进行委托。可能重复
Foo(Foo&&other)
other
的内部是左值,因为它有一个名称,这就是
Foo(Foo&other,int m)
被委托给的原因。使用
std::move(other)
创建一个右值引用,这样它就可以委托给
Foo(Foo&&other,int m)
而不是:
Foo(Foo&&other):Foo(std::move(other),0){}
重复谈论基本构造函数,但这里同样适用。复制构造函数应通过
const
引用获取其参数。