C++ 在唯一的PTR向量中转移所有权
我有两个班A和BC++ 在唯一的PTR向量中转移所有权,c++,vector,copy-constructor,unique-ptr,ownership,C++,Vector,Copy Constructor,Unique Ptr,Ownership,我有两个班A和B //A.h class A{}; // B.h typedef unique_ptr<A, AllocateA> APtr; typedef vector<APtr> BVEC; class B { public: BVEC vec; //error is here //.... }; 但我仍然收到相同的错误消息。那是因为unique\u ptr是。。。unique,它们指向一个对象的整个点,当unique_ptr超出范围时,它会删除它
//A.h
class A{};
// B.h
typedef unique_ptr<A, AllocateA> APtr;
typedef vector<APtr> BVEC;
class B
{
public:
BVEC vec; //error is here
//....
};
但我仍然收到相同的错误消息。那是因为unique\u ptr是。。。unique,它们指向一个对象的整个点,当unique_ptr超出范围时,它会删除它指向的变量。如果您可以轻松地将指向的变量分配给另一个唯一的\u ptr,那么何时将删除指向的变量?第一个超出范围还是第二个打开?这里没有“独特性” 这就是为什么不允许复制或分配unique_ptr,复制ctor和分配运算符被禁用
您正在寻找共享的\u ptr。多个shared_ptr可以指向一个变量,当所有变量都超出范围时,该变量将被删除,某种基本垃圾收集器此代码在gcc 4.9.2和Visual Studio 2013上运行良好:
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
using namespace std;
//A.h
class A{
public:
int alpha;
A(int input) : alpha(input){}
};
// B.h
typedef unique_ptr<A> APtr;
typedef vector<APtr> BVEC;
class B
{
public:
BVEC vec;
B(){}
const B& operator=(const B& b){
vec.clear();
for_each(b.vec.cbegin(), b.vec.cend(), [&](const unique_ptr<A>& i){vec.push_back(unique_ptr<A>(new A(*i))); });
return b;
}
B(const B& b){
vec.clear();
for_each(b.vec.cbegin(), b.vec.cend(), [&](const unique_ptr<A>& i){vec.push_back(unique_ptr<A>(new A(*i))); });
}
const B& operator=(B&& b){
vec.resize(b.vec.size());
move(b.vec.begin(), b.vec.end(), vec.begin());
return *this;
}
B(B&& b){
vec.resize(b.vec.size());
move(b.vec.begin(), b.vec.end(), vec.begin());
}
};
int main() {
B foo;
B bar;
for (auto i = 0; i < 10; ++i){
foo.vec.push_back(unique_ptr<A>(new A(i)));
}
bar = foo;
foo.vec.clear();
for (auto& i : bar.vec){
cout << i->alpha << endl;
}
foo = move(bar);
for (auto& i : foo.vec){
cout << i->alpha << endl;
}
return 0;
}
#包括
#包括
#包括
#包括
编辑:
在回顾了OP的标题之后,我觉得它可能是一个移动构造器。所以我也添加了其中一个。unique\u ptr无法复制。尝试共享。@user0175554什么是AllocateA
?请告诉我这不是你命名的删除器。如果你想转移所有权,你不能有一个复制构造函数。复制操作应使原始文件保持不变。为什么问题的标题指的是共享指针unique_ptr
与shared_ptr
不同,您的复制构造函数和赋值操作符应该使用const B&
参数。@MichałWalenciak,这与他的标题“转让所有权”一致,但与复制构造函数不一致。谢谢您,先生,@user3613500这听起来总是很好的解释谢谢你,没有AllocateA,我的程序就不能运行了。我使用共享的ptr加上你的代码,但我也同意你的答案是正确的,除了我自己的情况。
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
using namespace std;
//A.h
class A{
public:
int alpha;
A(int input) : alpha(input){}
};
// B.h
typedef unique_ptr<A> APtr;
typedef vector<APtr> BVEC;
class B
{
public:
BVEC vec;
B(){}
const B& operator=(const B& b){
vec.clear();
for_each(b.vec.cbegin(), b.vec.cend(), [&](const unique_ptr<A>& i){vec.push_back(unique_ptr<A>(new A(*i))); });
return b;
}
B(const B& b){
vec.clear();
for_each(b.vec.cbegin(), b.vec.cend(), [&](const unique_ptr<A>& i){vec.push_back(unique_ptr<A>(new A(*i))); });
}
const B& operator=(B&& b){
vec.resize(b.vec.size());
move(b.vec.begin(), b.vec.end(), vec.begin());
return *this;
}
B(B&& b){
vec.resize(b.vec.size());
move(b.vec.begin(), b.vec.end(), vec.begin());
}
};
int main() {
B foo;
B bar;
for (auto i = 0; i < 10; ++i){
foo.vec.push_back(unique_ptr<A>(new A(i)));
}
bar = foo;
foo.vec.clear();
for (auto& i : bar.vec){
cout << i->alpha << endl;
}
foo = move(bar);
for (auto& i : foo.vec){
cout << i->alpha << endl;
}
return 0;
}