I have an Animals class, and from this specific animals are derived. I have a list of animals called mItems. I want to see if my lions are hungry. I have created virtual functions in my Animals class:
virtual void IsLion() {return false;}
virtual void IsHungry() {return false;}
virtual void SetHungry(bool state) {}
In my Lion class I have extended these:
virtual void IsLion () {return true;}
virtual void IsHungry () {return mHungry;}
virtual void SetHungry () {mHungry = state;}
mHungry is a boolean member variable that will represent whether or not the lion is hungry.
void CSafari::KillHungryLion()
{
for(list<CAnimals *>::iterator i=mItems.begin();
i != mAnimals.end(); i++)
{
if((*i)->IsLion())
{
if((*i)->IsHungry())
{
mItems.remove(*i);
delete *i;
}
}
}
}
mItems is a list of pointers to CAnimal objects.
If a Lion is hungry, he is dead! The problem I am having is endless segfaults. I cannot pinpoint where I am going wrong. I have a function that is essentially equivalent to this that periodically updates the lions to being hungry, then I call this. It appears that it segfaults when I try to remove the items from mItems. Any ideas?