What does it mean to have sole ownership of object for unique_ptr?
Asked Answered
O

3

7

I know that std::unique_ptr is used when an object has only one owner and std::shared_ptr is used when an object has multiple owners. What does it mean to be the unique owner of an object?

Does being the unique owner mean that nobody else gets to see the object?

Can I return the raw pointer using the get() method when passing the pointer as a reference?

I feel that if I have to use get(), then I have either transfer ownership by using std::move, or use a shared pointer.

Overlap answered 11/11, 2019 at 14:32 Comment(0)
D
9

Ownership is all about: who cleans up the resource when it is no longer needed?

To be the only owner of a resource means that only you are responsible for deleting the object when it is no longer needed. This implies that nobody else can use the object after your lifetime ended. If others depend on the object still being alive after your lifetime ended you need shared ownership.

In modern C++, raw pointers should not participate in ownership. When you pass a raw pointer to a function you expect that this function will not hold on to this pointer to use it later.

Dearing answered 11/11, 2019 at 14:37 Comment(0)
N
2

Ownership talks about how is responsible about the lifetime of the object.

Let say you have a class shape that on which you can do various actions like, calculating the surface area, enlarging or shrinking that object, ...

And those tasks are performed by free functions calculate_shape_area, enlarge_shape, shrink_shape, and you pass the shape to that function, then those functions don't require ownership over that object, they only need to know the object while they are called. So you can (and should) pass that object as raw pointer:

struct shape {};

double calculate_shape_area(shape* obj) {
   // calculate the area, obj is only used within that function
   // and not stored anywhere else
}

int main() {
  auto obj = std::make_unique<shape>();
  auto area = calculate_shape_area(obj.get());
}

If you, on the other hand, want to store that shape in a scene graph using insert and that scenegraph, at a later step, displays all shapes. Then the scenegraph needs to know about that object after the call of insert so it either needs to create a copy of it or needs to claim ownership over it. And if you don't want to create a copy then you either need to move a unique_ptr to that insert or use shared_ptr.

Natascha answered 11/11, 2019 at 15:7 Comment(0)
G
0

But whet does it means to be the unique owner of an object?

Ownership means being responsible for the lifetime of the dynamic object (or more generally, a resource): When the owner ends the lifetime of the owned object, it is responsible for cleaning up (deallocate memory). Furthermore, the owner does not need to worry that lifetime of the object is ended by something else.

Being unique simply means that no other object can share the ownership.

Does being the unique owner means that nobody get to see the object?

No. Encapsulation is an orthogonal concept.

Giliana answered 11/11, 2019 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.