Hi all,
Im working on the design of a function at the moment and im not sure if the if statement i want to do is safe.
Ill start with the data structures i have as this is where the problem could come from. (simplified with relevent bits only)
class cPlayer
{
public:
bool inRound; //indicates is the player is participating
bool requiresAction; //indicates if the players needs to take further action this phase
int currentBet;
string name;
//....theres more but what i have is more than enough for this question
};
class cSeat //kind of a container class really
{
bool isOccupied; //set to true when a player instance is held
cPlayer *player;
};
class cTable//main class of the card game
{
protected:
cSeat[6]; //space for 6 players
};
so as you can hopefully see from the data structures i have a table which has 6 seats. upon each seat a player can sit so it has a pointer to that player and a bool to say if the seat is occupied. simple enough i think.
The code now that worries me is this.
//section of the loop im using
for(int x = 0; x<6; x++)
{
if((seat[x].isOccupied==true)&&(seat[x].player->inRound==true))//if the seat is occupied and the player is still playing this round
{
seat[x].player->currentBet = 0;
seat[x].player->requiresAction = true;
}
}
Now what im concerned about is that as far as i understand the if statment should stop evaluating if the first condition is false [as it is an && operation] (ie no player occupies the seat, therefore the pointer is invalid) so it should not try to evaluate the player->inRound bit which of course would be some nasty pointer access violation.
So to put it in words now i have set the scene, is using an evaluation like this a bad idea as it could potentially cause bad memory violations, or will it be ok if the bool isOccupied is set and cleared correctly without any bugs at all times? and secondly if this is bad code or quite dangerous to do things like this can you suggest a safer way to do such a thing?
Many thanks for your help again :)