0

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.

5 Answers 5

3

Is the assignment to currentPlayer in the play() function correct?

No. C++ pointer syntax is a little confusing as the * can mean several things. The expression Player *currentPlayer ; means that you're creating a pointer to a Player object (that is, currentPlayer is a variable that stores a memory address where a Player is located. When you say *currentPlayer, it means that you want the Player object stored at that memory address (this is called dereferencing the pointer). Saying currentPlayer = *player0 ; doesn't really make sense; it says to store a Player object (*player0) in an object that only stores memory addresses (the currentPlayer pointer). The solution, as has already been mentioned, is to say

currentPlayer=player0;

which basically means "set the memory address of the current player to be the same as that pointed to by player0." You can also think of it as "make currentPlayer point to the same thing player0 points to."

In regard to your other questions,

"will this code insert the correct player name into gameLog?"

Yes. *gameLog correctly dereferences the gameLog pointer.

Can I be sure passing currentPlayer to changeGmState will work the same as passing in player0 or player1?

Yes, assuming that currentPlayer has been set to either player0 or player1.

Sign up to request clarification or add additional context in comments.

Comments

3

You are dereferencing the pointer in the RHS here:

currentPlayer = *player0 ;
//              ^ dereference, gives object pointed at by player0

You need to assign a pointer, that is, to make the pointer on the LHS point to the same thing as the one on the RHS:

currentPlayer = player0 ;

Comments

3

The assignment should be :

if (player0First)
    currentPlayer = player0 ;
else
    currentPlayer = player1 ;

Pointer are just variable holding an address to a place in the memory. If you use the pointer variable alone, it will just be the address of the data.

*player0

When using this you are accessing to the data stored at the address in the memory.

After this assignment the variable currentPlayer will contain the same address as player0 (or player1) and if you use *currentPlayer you will access the same data (as it's the same address).

Note:

currentPlayer->name

This is just a shortcut for

(*currentPlayer).name

1 Comment

Thanks, I wanted to be sure of that.
2
if (player0First)
    currentPlayer = *player0 ;
else
    currentPlayer = *player1 ;

Careful here -- you're assigning both of these Player*s to Players

Get rid of the dereferences:

if (player0First)
    currentPlayer = player0 ;
else
    currentPlayer = player1 ;

Comments

2

You can use

currentPlayer = player0 ; //currentPlayer now contains the address contained by Player0 

currentPlayer = player1 ; //currentPlayer now contains the address contained by Player1 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.