gpt4 book ai didi

c++ - 如何移动 TArray 的内容

转载 作者:行者123 更新时间:2023-12-05 09:13:39 26 4
gpt4 key购买 nike

在 C++ 中我可以做到

class A
{
public:
A(std::vector<std::unique_ptr<int>> v) : _v(std::move(v)) {}
private:
std::vector<std::unique_ptr<int>> _v;
}

如何实现与 ue4 类型(TArrayTUniquePtr)类似的功能,即如何移动 TArray 的内容?


完整示例:

#include <cassert>
#include <memory>
#include <vector>
class MyObj {};
class A {
public:
A(std::vector<std::unique_ptr<MyObj>> v) : _v(std::move(v)) {}
auto GetV() { return _v.front().get(); }

private:
std::vector<std::unique_ptr<MyObj>> _v;
};

int main() {
auto v = std::vector<std::unique_ptr<MyObj>>();
v.push_back(std::make_unique<MyObj>());
A a(std::move(v));
assert(a.GetV());
}

最佳答案

TArray 有一个移动构造函数(即签名为 TArray(TArray &&other) 的构造函数),如 here 所示.

所以 std::move 应该像在 std::vector 上一样在 TArray 上工作。

关于c++ - 如何移动 TArray 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55819652/

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