PDA

View Full Version : Class Inheritence


recluse
11-17-2002, 09:04 PM
I'm having trouble getting the Player class/obj to change data in the TicTacToe class. I have the double array of squares in the TTT class so only one instance of it exists and both players put their pieces into that. But I can't get that to happen. Any pointers?

stuka
11-27-2002, 11:58 AM
Did you ever get this working? If not, the answer's pretty easy - you need to pass a reference/pointer to the Player in the constructor, so that you can call the SetPiece method on it in the Player class.

recluse
11-28-2002, 02:41 AM
I kind of understand. Passing the TicTacToe 'game' object as a reference (I'm not sure how to do it as a pointer) will let me be able to have 2 different players talking to the same 'game' object. But I'm not sure what I do with it in the constructor of any subsequent actions after that.

stuka
11-29-2002, 03:15 AM
What I've done in similar situations is have a pointer to the 'game' object as a member of the 'player' class. Then, when you construct the player class, you pass it a pointer to the game object, and store that pointer in the member variable. Then, whenever you need to do some operation on it, you use the pointer you stored. Say, for example, you haveclass player{
public:
void move(int x);

private:
game* board;
}

void player::move(int x){
board->set_space(x);
}