I have 2 static lists. EntityList::EnemyList, which is a list of enemies, and EntityList::MasterList, which is a list of pointers to ALL entities, like enemies, the player, bullets, etc.
In my factory function for entities, I have this code, which creates a new Enemy object:
This works fine, but as soon as I create more than one enemy, the pointer in MasterList apparently becomes invalid because the vector is resized and repositioned.Code:EntityList::EnemyList.push_back(Enemy()); EntityList::MasterList.push_back(&EntityList::EnemyList.back()); return &EntityList::EnemyList.back();
This code works, but there is a memory leak:
Is there a happy medium here? Neither works 100%. I was thinking smart pointers but those would just go out of scope after the function, no?Code:Enemy *ent=new Enemy(); EntityList::EnemyList.push_back(*ent); EntityList::MasterList.push_back(ent); return ent;