将所有权转移std::vector<unique_ptr<int> >到正在建造的阶级的正确方法是什么?
下面是我想要做的代码表示.我意识到它不正确(不会编译)并违反"唯一性",无论我是通过值还是通过引用将向量传递给构造函数.我希望Foo成为向量的新所有者,并希望调用函数放弃所有权.我需要构造函数std::unique_ptr<std::vector<std::unique_ptr<int> > >来执行此操作吗?
foo.h中
class Foo
{
public:
Foo(vector<std::unique_ptr<int> > vecOfIntPtrsOwnedByCaller);
private:
vector<std::unique_ptr<int> > _vecOfIntPtrsOwnedByFoo;
}
Run Code Online (Sandbox Code Playgroud)
Foo.cpp中
Foo::Foo(std::vector<std::unique_ptr< int> > vecOfIntPtrsOwnedByCaller)
{
_vecOfIntPtrsOwnedByFoo = vecOfIntPtrsOwnedByCaller;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激 - 我已经在网上搜寻正确的方法来做到这一点.谢谢!