Sign in to follow this  
ill

Any way to have C++ STL list not call destructor when removing value?

Recommended Posts

Yeah so I'm making a drawList kind of thing that all objects that are drawable, add themselves to this list each frame. The list handles sorting of the objects in proper draw order by depth which is why I'm using the STL linked list. Only problem is whenever the list has a value removed from it, the documentation says it always calls the object's destructor which I don't want. Like when it iterates through the draw list it should clear itself but not call the destructors of the things stored in the list since that would destroy those game objects. Is there a way to have the List do this withot calling destructors or do I have to make my own Linked List which I basically know how to do since I had to make it in Java, C, all that crap in all the classes I've been in.

Share this post


Link to post
Share on other sites
Don't add the actual object to the list then, just a pointer. Also, why clear and add everything each frame? Just resort the list itself.

Share this post


Link to post
Share on other sites
Quote:
Original post by ill
Only problem is whenever the list has a value removed from it, the documentation says it always calls the object's destructor which I don't want. Like when it iterates through the draw list it should clear itself but not call the destructors of the things stored in the list since that would destroy those game objects.


It would destroy the copies that you put into the draw list.

If that causes problems for the copied-from objects, then there is something wrong with either your copying process or your destruction process.

If your game objects store raw pointers to some kind of allocated resources, for example, consider instead using smart pointers.

Share this post


Link to post
Share on other sites
Funny thing is I thought of this as well over the course of the day. At the time I was coding it I was tired and didn't realize that storing just the pointer would not cause the original object to get destroyed.

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