Hello. I have an issue with a game I am programming for my c++ project which I am hoping one of you kind souls can help me solve...

I consider myself an intermediate beginner in c++. I am coding a space shoot-em-up in c++ with a proprietary graphics library provided by my university. I have set up a class for my aliens (imaginatively called Alien). I have need to track which aliens are on the screen at any one time, and to do this I have set up a list container which has pointers to the Alien objects, like so:

std::list<Alien*> alienList;

Although I haven't finished the bit of the code that will deal with the creation of the Alien objects, I envisage creating them dynamically and pushing them to the list (The parameters are not important):

alienList.push_back(new Alien(1, 1, 50));

In the main game loop I have need to loop through the list and update the positions of the on screen aliens. To do this I have set up an iterator which calls the class function updatePosition() for each Alien:

std::list<Alien*>::iterator iter;
for (iter = alienList.begin(); iter != alienList.end(); ++iter)
{
    iter._Ptr->_Myval->updatePosition();
}

My problem is this:

I need to delete my alien objects either when they are shot by the player, or they have travelled off the screen. I understand that because I have created these objects using the new keyword, then to delete them I need to use the delete keyword. But because these objects now live within my list, how do I go about this?
If I erase the nodes from the list (using list::erase) will this delete the actual objects themselves, or just the pointers on the list? Will the objects still exist somewhere, and if so, how can I go about deleting them and freeing the memory?
I fear I have gone out of my depth here. I should have listened to my tutor, who advised against the use of the STL before I fully understood how it worked. But when reading up on how lists work, they seemed exactly what I was looking for, and I am keen to understand how the whole thing works.
Any advice on this subject would be most appreciated.

Thanks


Richard

If you need random access, I might suggest a vector instead. If you are only using sequential access, a list will work.

Look into the function list::remove_if(). That should allow you to specify what "Alien"s to remove based on either postition or health.

If you need random access, I might suggest a vector instead. If you are only using sequential access, a list will work.

Look into the function list::remove_if().

I will do this. Many thanks for your reply.

Richard

>>If I erase the nodes from the list (using list::erase) will this delete the actual objects themselves, or just the pointers on the list.

It will delete the object, you don't need to worry about freeing the memory any longer.

>>If I erase the nodes from the list (using list::erase) will this delete the actual objects themselves, or just the pointers on the list.

It will delete the object, you don't need to worry about freeing the memory any longer.

Excellent! This is what I needed to know. I was worried that I might be leaving a trail of destruction behind me as the game continued. Many thanks for your replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.