Does this mean that I have bad design?
Yes, but it is a starting point.
Let's consider some of the smart pointers availabe to use.
unique_ptr
- a single owner exists that is responsible for disposing of the object.
shared_ptr
- many (or potentially many) owners exist and the last one must dispose of the object
weak_ptr
- many owners may exist but this is not one of them, the weak pointer may out live the object pointed to, if the object pointed to is disposed of the weak pointer will be null (that is the lock method will return a null shared_ptr)
observer_ptr
(n3840)- Not yet part of the standard so C-style pointers (T*) can be used instead if needed. These work very much like a weak_ptr, but it is the programmer’s responsibility to make sure that all observers are not dereferenced after the object pointed to is disposed of.
A solution is to split the design into an object that will own all the pieces and the pieces (the cycle nodes). The owning object can use shared_ptr
or unique_ptr
to automatically manage the life time of the nodes. The nodes themselves can refer to each other with weak_ptr
, observer_ptr
, or a Reference (Node&)
struct Node { whatever data; vector<Node_ID> nodes; }
and using a table to retrieve actual nodes is what I was talking about.