I prefer to use the primitive array: there will not need to be that many monsters in play at a time, so I wouldn't save space, and I also think an array is easier to use.
A std::vector is likely to be easier than a manually managed dynamic array.
For example, we can write a function object that tells us if the foe is dead:
Code:
// Foe class example
class Foe
{
public:
int hitPoints() const;
// ...
private:
// ...
};
// function object class example
class FoeIsDead
{
public:
bool operator()(const Foe& foe) const
{
return foe.hitPoints() <= 0;
}
};
Now, we can do what you wanted to do, in a single line:
Code:
std::vector<Foe> foes; // vector of foes
// ...
// remove the foes that are dead
foes.erase(std::remove_if(foes.begin(), foes.end(), FoeIsDead()), foes.end());
Instead of having to keep track with a nummonsters variable, we just use foes.size() if necessary.
One more thing: is the way I'm doing it going to cause memory leaks, since I haven't properly deleted the data before overwriting it? I've done most of my OO programming in Java, so am not sure how much memory management C++ does on its own.
Overwriting does not cause memory leaks. What would cause a memory leak is if delete[] is not called for the dynamic array, but if you use a std::vector, then the delete[] is done for you automatically.