Jump to content
  • Advertisement
Sign in to follow this  
aaron_ds

Heterogenous lists/trees

This topic is 5460 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'm currently using C++ in my project and came across the need for a heterogenous list/tree. A tree would be perferable, and each node should be able to handle a variable number of children. I'm trying to think of ways to consturct one. I have some experience with templates, and just got two books on them, so I'm willing to get my hands dirty with them. Maybe this isn't possible since C++ is a strongly typed language, but it would still be very satisfying to get a heterogenous tree working.

Share this post


Link to post
Share on other sites
Advertisement
I've gotten something like this working, at least in an ugly fashion. Using virtual functions that are common to each of the elements which may appear in the tree makes things much nicer, but isn't always do-able.

Share this post


Link to post
Share on other sites
Lots of ways of having heterogeneous STL containers, some ideas:

type hierarchies storing a pointer of base type in the containers, or:

a STL container of boost::any

If you need an STL style n-ary tree you can have a look at the bottom of here.

Share this post


Link to post
Share on other sites
I'm not too sure what you mean by heterogenous. Are you wanting a tree where each node can potentially have an unlimited number of children, not each child of a node is not necessarily of the same data type??

if so make class node and abstract base type with a child list of type node pointers. Then each type of special node you want should be derived from node.

--Ninj4D4n

Share this post


Link to post
Share on other sites
Quote:
Original post by Ninj4D4n
I'm not too sure what you mean by heterogenous.


heterogeneous container means a container that stores instances of different types, instead of homogenous container which means a container that stores instances of only one particular type.

Share this post


Link to post
Share on other sites
Quote:
Original post by aaron_ds
I'm currently using C++ in my project and came across the need for a heterogenous list/tree.
A tree would be perferable, and each node should be able to handle a variable number of children.
I'm trying to think of ways to consturct one. I have some experience with templates, and just got two books on them, so I'm willing to get my hands dirty with them.
Maybe this isn't possible since C++ is a strongly typed language, but it would still be very satisfying to get a heterogenous tree working.

Use a homogeneous tree of references\pointers to the common base class of all the stuff you'd like to store in the tree.

Or use void pointers :P

Share this post


Link to post
Share on other sites
Quote:
Original post by SoulSkorpion
Use a homogeneous tree of references\pointers to the common base class of all the stuff you'd like to store in the tree.


well its not technically not possible to store reference types directly, you'll have to wrap up a reference of some type in a user-defined type.

Quote:
Original post by SoulSkorpion
Or use void pointers :P


You do that then you'll never know how to delete it properly later on if you use it to store arbitrary types unless you save the type information some where, you could use boost::shared_ptr<void> because it does know how to delete it properly because it gets the type of the instance at construction time, or the other alternative like i said was to have a container of boost::any.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!