I'm working on the data structure of objects in my 2D game engine, such as sprites, score displays etc. The data structure is a tree, where each node owns its children. The root node could be a Level, for instance, which contains a BigBoss object, which in turn contains Sprites that make up the boss. (I will use the words node and object interchangeably in this post.)
I know that I want a tree structure, but I'm not sure about the implementation details, so I invite suggestions and comments. Please note that my focus at this stage is correctness, not speed. Also note that my tree structure supports an editor, where objects can be added and deleted (and reparented) at any time.
Here are my thoughts so far:
* My Node baseclass stores its children as std::shared_ptr in a container (probably a std::vector). The node owns its children, but I'm using shared_ptr instead of unique_ptr to allow other nodes to reference it using weak_ptr.
* If a Node wants to keep a reference to another node, it stores it in a weak_ptr. For instance, a Snake node might want to keep track of a Food node, even if the food is not in the same subtree as the Snake. If the Food disappears, the Snake will notice that when it runs its updatePath routine, as foodWeakPtr.lock() will return null, and it will stop chasing it.
* Getters, such as objectWithName(const std::string& name) or trackedObject(), return (lists of) weak_ptr<Node>, pointing to item(s) in the shared_ptr collection.
Does this sound like a sound () way to do it? I'd appreciate comments from people who have experience with this kind of thing. I'm new to the whole smart pointer thing.
Thanks in advance!