Sign in to follow this  
etsuja

dynamic class objects

Recommended Posts

etsuja    100
I'm trying to make a game with dynamic objects creation/deletion but I was wondering if I should make a struct linked list and have the new object created in there or should I make the class object itself a linked list? I forgot to mention that I also need direct access to each one of the items so they need some sort of identificatin so I don't have to search through the entire linked list everytime i want to update an object.

Share this post


Link to post
Share on other sites
meink    133
Hard to say without more requirements, but I would recommended against combining linked list code into your game object. This is a fundamental tenet of OO design - keep your object focused on a specific function.

The only time I have found that you actually need access to the list from within the object is when it needs to delete itself. To acheive this, have a "dead" flag that it sets, and then the engine can purge dead objects whenever convenient.

Share this post


Link to post
Share on other sites
Razor89    134
What about Std::Vector<T>? Although I don't know much about how Vectors perform
in a gaming environment but you do have the option of dynamically adding and removing
objects and being able to access it with an index.

On the other hand you could just have a empty array of a predefined size
and simply search for an null element when allocating an object.

Share this post


Link to post
Share on other sites
_goat    804
What you're looking for in this case is std::vector. They allow random access, which linked lists don't. In this case, they are much faster for accessing than linked lists. They are slower than linked lists for inserting, deleting and expanding. However, only a true idiot would make heavy use of those features with a vector and expect reasonable performance. Their pros are very real - contiguous data and random access are necessary.

Go give it a look. Hope that helps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this