gpt4 book ai didi

c++ - 移动构造函数 C++

转载 作者:行者123 更新时间:2023-11-30 00:47:25 24 4
gpt4 key购买 nike

移动构造函数的正确方法是什么?

class A{
...some stuff...
private:
int i;
std::string str;
};

A::A(A &&a)
{
*this = std::move(a);
};

A::A(A &&a)
{
this->str = std::move(a.str);
};

在第二种情况下,对 std::move() 的 int 值有用吗?

最佳答案

应该是

A::A(A&& other)
: i{other.i},
str{std::move(other.str)} {
// nop
}

这是移动构造函数的默认实现。

关于c++ - 移动构造函数 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34967768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com