I have a directed acyclic graph, composed of Node objects. Each node has a list of std::shared_ptr
s to other nodes, which are its children in the graph. I have lots of useful methods I need, such as inserting/emplacing/reparenting nodes, testing if a node is an ancestor of another, etc. Some are standard STL-like methods, and some are specific to directed acyclic graphs and specific to my needs.
The question is, When such a method takes a node as a parameter, should it take a reference? of a weak_ptr
? or a shared_ptr
? I tried to examine use cases but it's hard to tell. What's the best design here? I'm new to smart pointers and I'm not sure what's the best choice. Should I treat shared_ptr<Node>
as "the representation" of node objects? Or maybe the way to choose is more sophisticated?
Thanks in advance