C++: managing a set of objects so that the held objects can access the data structure holding them

I store pointers to all instances of class Cell in a linked list. The std::list<Cell*> cells is stored in a class named Game. The method Game::update() iterates trought all instances of Cell held in the list cells, and calls each cell's Cell::update() method. If the Cell::update() method finds out at some point that the cell is ready to divide, it needs to push_back() a pointer to the new cell to cells in order for the new cell to be updateable. But how can I refer to Game::cells within the cell in order to do that? I have considered the following options:

  • Defining std::list<Cell*> cells in global scope instead.
    • nasty and makes it impossible to have more than one Game
  • Making std::list<Cell*> cells a static member of Cell
    • better than the global var but still prevents the use of multiple Game instances
  • Passing a reference to the current game's Game::cells as a parameter to the Cell's constructor and subsequently storing a copy of that reference in the newly constructed cell instance
    • I think it is a little inflexible
  • Passing a reference to the current game's Game::cells as a parameter to the Cell::update() method

Are there more elegant solutions to utilize?


I would not introduce an unnecessary dependence between Cell and Game, if all Game does is to call methods on Cells and hold instances of them.

You could instead return new Cells (or none) from Cell::update() and let Game decide what to do, most probably add them to the list. You could also define a new function for this in Cell, something like Cell::splitCell() and let Cell::update() only update properties of Cell objects (as the name would suggest).

I would probably pass a reference to the Game into Cell::update() and then tell the Game object to divide the cell, but other options may be better as I still don't think I have a full picture of what your overall goal is.

Pass a reference to Game into the Cell constructor. Then make a function that lets a Cell push other Cell objects onto the list (or make Cell a friend class of Game so that it can access the list directly).

This way, Cell can decide when it needs to split, but it can delegate the action to the Game object where it belongs.

Also, if there are any future thing that the Cell might want the Game to do for it, he already has a hold of the right object.

If Cell objects must know which Game object they belong to, derive a class from std::list<> and include a pointer to Game (Game *) as a member of Cell. Cell object can then use this pointer to add another Cell to th Game.

class Game
      class Cell
           Game *game;
      class Cells : public std::list<Cell *>
            Game *game;
            operator += (Cell *cell) {cell->game = this->game; ....}
      Cells cells;

Need Your Help

ConcurrentDictionary - AddOrUpdate issue

c# concurrentdictionary

I'm using this code below to try to update the values in a dictionary object depending on its key.

Change button color on click

javascript jquery htmlbutton

I want to know if is possible to attain the following scenario: