简体   繁体   English

将对象的所有权从一个unique_ptr转移到C ++ 11中的另一个unique_ptr?

[英]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. 所有权转移后,放弃所有权的智能指针变为nullget()返回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 : 与具体子类CatDog

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 . 在构建Housepet变量将为nullptr因为我们已将宠物的所有权转让给House

Live demo 现场演示

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::movestd::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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM