Tree Node With Mutiple Parents

Started by
0 comments, last by Zumichu 20 years, 10 months ago
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?
[I did absolutely nothing, and it was everything that I thought it could be]
Advertisement
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.

This topic is closed to new replies.

Advertisement