   Vector of pointers, will this work right?
  I'm trying to implement polymorphism on an enemy system that I am currently working on. I plan to use a vector container to store all of my enemies. All of my enemy classes are derived from a base class called Enemy. So to achieve polymorphism, I need to represent each enemy as a pointer to an Enemy class, and allocate memory according to what enemy it is going to be. To show what I'm talking about:// Here is my enemy vectorstd::vector<Enemy*> enemies;// I want to create a new ninja enemyenemies.push_back(new Ninja());The problem comes when I need to free the memory allocated for the enemies at the end of each level. I want to make sure that I don't leak any memory, so please tell me if this method will work fine:// End of level, so free the memoryfor(int i = 0; i < enemies.size(); i++)    delete enemies;// Clear the vector of enemiesenemies.clear();Is this the correct way that I should be freeing the enemy pointers? I'm simply worried that I might cause a memory leak using vectors and pointers like this.Thanks!   
  Well, you probably want to maintain a proper vector at all times without "dead" entries and with a correct size, just in case an actor's destructor decides to do anything fancy (involving "child" enemies, such as bullets). And for the same reason you should probably delete them in reverse order.   ;  Ok. I'll keep that in mind because although the enemies don't have a bullet system now, they will soon. I need to be careful then about freeing the bullet classes that will be within the enemy classes too.;  you might also want to look into either a vector of boost::shared_ptr or a boost::ptr_vector,both of which will take care of the memory management for you.;Quote:Original post by ChaosEngineyou might also want to look into either a vector of boost::shared_ptr or a boost::ptr_vector,both of which will take care of the memory management for you.The boost::ptr_vector looks like my best bet in this situation.Thanks!;  I just want to chime in that what you are doing in your original post is essentially what I do most of the time.  You will probably want to inject a little more smart management logic into your code but as far as creation and deletion, this will work.  Be sure to use virtual destructors for your Enemy class by the way.   ;Quote:Original post by helixI just want to chime in that what you are doing in your original post is essentially what I do most of the time.  You will probably want to inject a little more smart management logic into your code but as far as creation and deletion, this will work.  Be sure to use virtual destructors for your Enemy class by the way.That's good to hear. I just want to be careful to not cause memory leaks, as I don't usually handle game objects like this.   