Jump to content
  • Advertisement


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


Tree Node With Mutiple Parents

This topic is 5615 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
	virtual ~Node();

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

	Node* GetParent() const;

	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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!