This is a good specific example of a very common question about how to use smart pointers in data structures:
weak_ptr VS shared_ptr in graph node parent list
I have a directed acyclic graph implemented by Graph and Node classes. Each node has a list of pointers to children and a list of pointers to parents. [...] The Child list uses
std::shared_ptr
so that nodes are kept in memory at least as long as they have parents. But I don't want a node to own its parents, so I usedweak_ptr
for the pointers to parents.But then there was a problem with the algorithms...
Add a Comment
Comments are closed.