[英]Transferring the ownership of object from one unique_ptr to another unique_ptr in C++11?
In C++11
we can transfer the ownership of an object to another unique_ptr
using std::move()
. 在C++11
我们可以使用std::move()
将对象的所有权转移到另一个unique_ptr
。 After the ownership transfer, the smart pointer that ceded the ownership becomes null
and get()
returns nullptr.
所有权转移后,放弃所有权的智能指针变为null
, get()
返回nullptr.
std::unique_ptr<int> p1(new int(42));
std::unique_ptr<int> p2 = std::move(p1); // Transfer ownership
What are the situations where this will be useful as it is transferring the ownership to another unique_ptr
? 在将所有权转移到另一个unique_ptr
,这会有什么用处?
The following situations involve transferring ownership from one unique_ptr
to another: returning from a function, and passing as a parameter to a function like a constructor. 以下情况涉及将所有权从一个unique_ptr
转移到另一个:从函数返回,并作为参数传递给类似构造函数的函数。
Say you have some polymorphic type Animal
: 假设你有一些多态类型Animal
:
struct Animal {
virtual ~Animal() {}
virtual void speak() = 0;
};
with concrete subclasses Cat
and Dog
: 与具体子类Cat
与Dog
:
struct Cat : Animal {
void speak() override { std::cout << "Meow!\n"; }
};
struct Dog : Animal {
void speak() override { std::cout << "Woof!\n"; }
};
And you want a simple factory that creates a pet based on a required value of obedience. 而且你想要一个简单的工厂,根据所需的服从价值创造宠物。 Then the factory must return a pointer. 然后工厂必须返回一个指针。 We want the pet factory to transfer ownership of the created pet to the caller so a reasonable return type is std::unique_ptr<Animal>
: 我们希望宠物工厂将创建的宠物的所有权转移给调用者,因此合理的返回类型是std::unique_ptr<Animal>
:
std::unique_ptr<Animal> createPet(double obedience) {
if (obedience > 5.0)
return std::make_unique<Dog>();
return std::make_unique<Cat>();
}
Now, say we want to create a House
that will own the pet then we might want to pass the pet into the constructor of the House
. 现在,假设我们要创建一个拥有宠物的House
,那么我们可能想要将宠物传递给House
的构造者。 There is some debate ( see comments on this blog post ) about how best to pass a unique_ptr
to a constructor but it would look something like this: 关于如何最好地将unique_ptr
传递给构造函数,有一些争论( 请参阅此博客文章的评论 ),但它看起来像这样:
class House {
private:
std::unique_ptr<Animal> pet_;
public:
House(std::unique_ptr<Animal> pet) : pet_(std::move(pet)) {}
};
We have passed the unique_ptr
into the constructor and have then "moved" it to the member variable. 我们已将unique_ptr
传递给构造函数,然后将其“移动”到成员变量中。
The calling code could look something like: 调用代码可能类似于:
auto pet = createPet(6.0);
House house(std::move(pet));
After constructing the House
, the pet
variable will be nullptr
because we have transferred ownership of the pet to the House
. 在构建House
, pet
变量将为nullptr
因为我们已将宠物的所有权转让给House
。
for example if you call a function you can move
your unique_ptr
in the parameter list so it can be a part of your function signature 例如,如果调用函数,则可以在参数列表中move
unique_ptr
,使其成为函数签名的一部分
foo ( std::unique_ptr<T>&& ptr )
you can call foo with 你可以打电话给foo
foo( std::move(myPtr) );
Note that std::move
is an unconditional cast and unique_ptr
is an object with a state, and a part of that state is the pointer that that unique_ptr
is managing, with std::move
you are casting the entire object, you are not really changing anything about ownership, there is nothing peculiar about std::unique_ptr
while using std::move
because std::move
doesn't really care about anything specific, as I said it is an unconditional cast and unique_ptr
simply gets casted, the entire object that is an instance of type unique_ptr<T>
is casted . 请注意, std::move
是一个无条件转换, unique_ptr
是一个具有状态的对象,该状态的一部分是该unique_ptr
正在管理的指针,使用std::move
你正在转换整个对象,你不是真的改变任何关于所有权的东西,使用std::move
时std::unique_ptr
并没有什么特别之处,因为std::move
并不真正关心任何特定的东西,因为我说它是一个无条件的unique_ptr
而且unique_ptr
只是被渲染,整个作为unique_ptr<T>
类型的实例的对象被转换。
If you want to talk about a transfer of ownership of the object pointed by your unique_ptr
, you should consider the swap
provided by std::unique_ptr<T>
itself . 如果你想谈谈你的unique_ptr
指向的对象的所有权转移,你应该考虑std::unique_ptr<T>
本身提供的swap
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.