A std::shared_ptr destroys the object it's managing when the reference count hits 0. However, I'm looking for a type of smart pointer where the object gets destroyed when the reference count drops below 2. Is there a smart pointer that will behave like this (or can I make a smart pointer behave like this, in a safe way)?
Use case scenario: I'm modelling a connection. The connection is owned (as in "ownership by a smart pointer") by the two end points it connects. As soon as one of the end points gets destroyed, the connection should be destroyed as well.
I know I could achieve this with classic delete statements in the appropriate destructors (since my requirement of "below 2" is super easy in this case). But I think this is a valid use case for a type of smart pointer, and I'm curious to see if I could do this using a modern way.
Aucun commentaire:
Enregistrer un commentaire