title: C++ Object or 2-dimensional Array as a Parameter?
body: <p>In a Tic Tac Toe game I creating for school I have five classes: Game (acts as a controller for the game), Player (base class), HumanPlayer and AIPlayer (both derived from Player), and Board. </p>
  
  <p>Board contains a 2D array and some functions for displaying and working on the array. All three player classes have a function <code>makeAMove(Board &amp;board)</code> which will choose a position on the board and place an X or an O. </p>
  
  <p>As you can see <code>makeAMove</code> accepts a <code>Board</code> object as a parameter. Then the function will decide on a move and alter the Board object's board array. If I'm understanding correctly, the actual object being passed in will be altered and there will not be any need to return anything back. </p>
  
  <p>Am I understanding this correctly? Would it be better to use the 2d array as the parameter instead?</p>
  
  <p>Here is a condensed version of my code.  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt; // auto_ptr
  
  class Board
  {
      public:
          Board()
          {
              for (int row = 0; row &lt; 3; row++)
              {
                  for (int column = 0; column &lt; 3; column++)
                      board[row][column] = ' ';
              }
          }
  
          void displayBoard()
          {
              std::cout &lt;&lt; "\n-------------" &lt;&lt; std::endl;
  
              for (int row = 0; row &lt; 3; row++)
              {
                  std::cout &lt;&lt; "| " ;
                  for (int column = 0; column &lt; 3; column++)
                      std::cout &lt;&lt; board[row][column] &lt;&lt; " | ";
                  std::cout &lt;&lt; "\n-------------" &lt;&lt; std::endl;
              }
          }
  
  /*      My thought was to use getBoard to return the array rather than using the 
          object as a parameter       
          char getBoard()
          {
              return board[][3];
          }       
  */
  
      char getCell(int row, int column)
      {
          return board[row][column];
      }
  
      void setCell(int row, int column, char player)
      {
          board[row][column] = player;
      }
  
      private:
          char board[3][3];
  
  };
  
  class Player
  {
      public:
          virtual void makeAMove(Board &amp;board) = 0;   
  };
  
  class AIPlayer : public Player
  {
      virtual void makeAMove(Board &amp;board)
      {
          // do some work ont the board array
      }   
  };
  
  class HumanPlayer : public Player
  {
      virtual void makeAMove(Board &amp;board)
      {
          // do some work on the board array
      }
  };
  
  class Game 
  {
      public: 
          Game(unsigned int players)
          {
              if (players == 1)
              {
                  player1.reset (new HumanPlayer()); 
                  player2.reset (new AIPlayer());
              }
              else 
              {
                  player1.reset (new HumanPlayer());
                  player2.reset (new HumanPlayer());
              }
          }
  
          void playGame()
          {
              player1-&gt;makeAMove(myBoard);
          }
  
      private:
          std::auto_ptr&lt;Player&gt; player1; // pointer to a Player object (C++11 has more secure unique_ptr)
          std::auto_ptr&lt;Player&gt; player2; // pointer to a Player object
          Board myBoard;
  };
  
  int main() 
  {
      Game myGame(1);
      myGame.playGame();
  
      return 0;
  }
  </code></pre>