yeah, the result is kind of the same as an overload, but conceptually, given it's run time dynamic behavior, it is also completely different.
Do you understand why you have a vector of
pointers to enemies, rather than actual enemies? There are multiple reasons, which involve slicing, static typing, and the fact that virtual calls are only resolved when called through pointers (->) (well, it also works with references, but this is less recommended, from a maintenance point of view).
Usually, you do something like this:
Code:
body* bodyHunter = new hunter
However, this is rife with potential memory leaks. It is easier to create an auto object, and point to that:
Code:
{
hunter actualHunter
body* bodyHunter = &actualHunter
//actualHunter is destroyed here
}
This is why you have a vector of Badgers/Zombies etc. It also has the advantage of less calls to new.
This is how you populate it, I guess.
Code:
std::vector<Zombie> zombies(zomb);
std::vector<Body*> aEnemies;
std::vector<Zombie>::iterator it = zombies.begin();
std::vector<Zombie>::iterator itEnd = zombies.end();
{
aEnemies.push_back(&*it);
}
... ditto with Hunters etc.
&*it means "the address of where it points" This is usually not needed, but sometimes the type of it is not an actual pointer. For example, you always have to do this for lists. With vectors, you can usually get away with using it straight up.
(I'm sure there are efficient std algorithms to do this, but that's besides the point)
On a side note, in the above code I had written, the correct code is:
Code:
std::vector<Body*>::iterator it = aEnemies.begin();
std::vector<Body*>::iterator itEnd = aEnemies.end();
for( ; it!=itEnd ; ++it )
{
if ((*it)->isHumanInRange(a_man))
{
(*it)->hunt(a_man);
}
else
{
(*it)->move();
}
}
This is because it is an iterator to a pointer, not an iterator to an object.