I am not sure how to approach this problem:
'Player' class mantains a list of Bullet* objects:
class Player
{
protected:
std::list< Bullet* > m_pBullet_list;
}
When the player fires a Bullet, it is added to this list. Also, inside the constructor of bullet, a reference of the same object is updated in CollisionMgr, where CollisionMgr also mantains a list of Bullet*.
Bullet::Bullet(GameGL*a_pGameGL, Player*a_pPlayer)
: GameObject( a_pGameGL )
{
m_pPlayer = a_pPlayer;
m_pGameGL->GetCollisionMgr()->AddBullet(this);
}
class CollisionMgr
{
void AddBullet(Bullet* a_pBullet);
protected:
std::list< Bullet*> m_BulletPList;
}
In CollisionMgr.Update(); based on some conditions, I populate class Cell
which again contain a list of Bullet*. Finally, certain conditions qualify a Bullet to be deleted. Now, these conditions are tested upon while iterating through a Cell's list. So, if I have to delete the Bullet object, from all these places, how should I do it so that there are no more dangling references to it?
std::list< Bullet*>::iterator bullet_it;
for( bullet_it = (a_pCell->m_BulletPList).begin(); bullet_it != (a_pCell->m_BulletPList).end(); bullet_it++) {
bool l_Bullet_trash = false;
Bullet* bullet1 = *bullet_it;
// conditions would set this to true
if ( l_Bullet_Trash )
// TrashBullet( bullet1 );
continue;
}
Also, I was reading about list::remove, and it mentions that it calls the destructor of the object we are trying to delete. Given this info, if I delete from one list, the object does not exist, but the list would still contain a reference to it..How do I handle all these problems ? Can someone please help me here ?
Thanks
PS: If you want me to post more code or provide explanation, please do let me know.