Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

c++ - 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(). After the ownership transfer, the smart pointer that ceded the ownership becomes null and get() returns 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?

question from:https://stackoverflow.com/questions/26318506/transferring-the-ownership-of-object-from-one-unique-ptr-to-another-unique-ptr-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Say you have some polymorphic type Animal:

struct Animal {
  virtual ~Animal() {}
  virtual void speak() = 0;
};

with concrete subclasses Cat and Dog:

struct Cat : Animal {
  void speak() override { std::cout << "Meow!
"; }
};

struct Dog : Animal {
  void speak() override { std::cout << "Woof!
"; }
};

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

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.

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.

Live demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...