I'm completely new to C++, and my only experience with programming is with Java. I'm still trying to get a handle on pointers. How would I assign a pointer to point at the same object that a second pointer points at? Specifically, I want to assign currentPlayer to point at player0 or player1, and switch back and forth as the game state changes.
using namespace std ;
struct Player {
public:
string name ;
//fields
};
class Game {
private:
Player *player0 ;
Player *player1 ;
Player *currentPlayer ;
bool player0First ;
stringstream *gameLog ;
//more fields
public:
void play() ;
void changeGmState(Player*) ;
//other member functions, etc.
} ;
void Game::play() {
if (player0First)
currentPlayer = *player0 ;
else
currentPlayer = *player1 ;
*gameLog << "It's " << currentPlayer->name << "'s turn!" << endl ;
changeGmState(currentPlayer) ;
//code
}
Is the assignment to currentPlayer in the play() function correct? Xcode seems to think not, but I'm still horribly confused by pointer assignment and proper use of the dereference operator. Related questions: will this code insert the correct player name into gameLog? Can I be sure passing currentPlayer to changeGmState() will work the same as passing in player0 or player1? Any other feedback is appreciated. Thanks in advance for your help.