Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/286230868509462528
Adjusted the title to match the question, reformatted code
Source Link
Trevor Powell
  • 21.5k
  • 1
  • 63
  • 96

c++ delete objects on the screen How to remove an object from a std::vector

So I have a vector of bullets that show up on the screen and I do not want these bullets to live forever. I want them to "die as soon as they go off screen or collide with an enemy. How can I make sure they are removed from the array and no longer take up memory.

iecode:

std::vector<Bullet> bulletArray;

for(int i=0; i<bulletArray.size(); i++)
        {
            bulletArray[i].MoveBullet();
            bulletArray[i].Draw();
            if(bulletArray[i].PosY<0)
            {
                        //Delete object forever 
            }
        }

c++ delete objects on the screen

So I have a vector of bullets that show up on the screen and I do not want these bullets to live forever. I want them to "die as soon as they go off screen or collide with an enemy. How can I make sure they are removed from the array and no longer take up memory.

ie

std::vector<Bullet> bulletArray;

for(int i=0; i<bulletArray.size(); i++)
        {
            bulletArray[i].MoveBullet();
            bulletArray[i].Draw();
            if(bulletArray[i].PosY<0)
            {
                        //Delete object forever 
            }
        }

How to remove an object from a std::vector

So I have a vector of bullets that show up on the screen and I do not want these bullets to live forever. I want them to "die as soon as they go off screen or collide with an enemy. How can I make sure they are removed from the array and no longer take up memory.

code:

std::vector<Bullet> bulletArray;

for(int i=0; i<bulletArray.size(); i++)
{
    bulletArray[i].MoveBullet();
    bulletArray[i].Draw();
    if(bulletArray[i].PosY<0)
    {
        //Delete object forever 
    }
}
remove SDL tag, question does not involve SDL
Link
Sean Middleditch
  • 42k
  • 4
  • 91
  • 133
Source Link
joncodo
  • 293
  • 1
  • 5
  • 17

c++ delete objects on the screen

So I have a vector of bullets that show up on the screen and I do not want these bullets to live forever. I want them to "die as soon as they go off screen or collide with an enemy. How can I make sure they are removed from the array and no longer take up memory.

ie

std::vector<Bullet> bulletArray;

for(int i=0; i<bulletArray.size(); i++)
        {
            bulletArray[i].MoveBullet();
            bulletArray[i].Draw();
            if(bulletArray[i].PosY<0)
            {
                        //Delete object forever 
            }
        }