Archived

This topic is now archived and is closed to further replies.

Tree Node With Mutiple Parents

This topic is 5306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a basic tree structure:
class Node
{
public:
	Node();
	virtual ~Node();

	void AddChild(Node* node);
	void DeleteChild(Node* node);	

	Node* GetParent() const;

        //...
	
protected:
	
	Node* m_Parent;	
	vector m_Children;
};
 
Cool, everything works great, it recursively deletes the whole tree when the root node is deleted, and it works with mixed derived types. The problem is I started allowing Nodes to have multiple Parents (sometimes this is useful). When the tree goes to delete it''s self, a child node may already have been deleted by a different parent. So I thought just replacing the child pointers with ref counted pointers would work. Well, I tried the boost shared_ptr, but it doesn''t seem to support inheritance well, i.e. if I try to add a child of a derived type, I get a compile error saying the conversion is not supported. Is there a better way to do what I''m trying to do, or am I just not using boost::shared_ptr correctly?

Share this post


Link to post
Share on other sites
That''s not a tree, it''s a graph. And you''ve just entered the hellish world of garbage collection. Ref-counting should work for now, but keep in mind what happens when a node is its own parent (or the parent of a node who is its parent).

Why would you ever inherit from/subclass a node, though? Have a member of the node class be the data, which can be subclassed.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites