3 ms·
Why not use shared_ptr in this case? Why enforce single ownership?
by bluecmd 11y ago
Why not use shared_ptr in this case? Why enforce single ownership?
- malkia 11y agoYou also need week_ptr, that holds a pointer for a week... just kidding... But often you need weak_ptr, for caches - where shared_ptr won't free it, and unique_ptr won't allow it to be shared.
- nice_byte 11y agoBecause the example is about an idiomatic way of enforcing exclusive pointer ownership. Maybe it wasn't a very good example though. A better example might be this: in the Builder design pattern, an object is built from various "parts" by the client. It's obvious that the object should have an exclusive ownership of all of its parts. However, at the time of creation, each part belongs to whoever created it, so the ownership needs to be transferred.