I'm having a little problem where I want two different classes to be able to share and make changes to another class object.
I've got a HumanPlayer class, a ComputerPlayer class, and a Board class. The two player classes need to be able to interact with the one Board class. I thought that I could pass a reference to the same Board object to each class but it doesn't appear to be working the way I want it to. Here's part of what's in main and I'll describe what's happening as best I can:
//main.cpp
Board *board2 = new Board();
board2->setBoardSize(5);
board2->initPits();
HumanPlayer firstPlayer(*board2, *menu, menu->askForFirstTurn(), true);
firstPlayer.removeFromPit(3);
board2->showBoard();
firstPlayer.removeFromPit(3); is supposed to just set a value in an array in the board class to zero. And it does that. If I were to show the board from within the code of the FirstPlayer class, it would show the change. But when I call board2->showBoard() it's as if nothing was changed. It's still the original unchanged board. What I really want to have happen is to have my firstPlayer and secondPlayer classes work on one shared board object. I'm just not sure how to implement this properly now.
Thank you all for your help. Let me know if you need more information.
HumanPlayer firstPlayer(*board2, *menu, menu->askForFirstTurn(), true);is undefined behavior.HumanPlayer firstPlayer(*board2, i, ++i, true);ifaskForFirstTurnchanges the state of the object, and*menuis passed by value, then behavior could be undefined.