I am writing a card game (poker) app as a learning project and have a question about deleting pointers. A bit of background first:
I have a class Player, which I want to initialize certain number of times, depending on user input. The constructor of class Player deals the cards, then checks the board (dealt in different class), checks what final hand player has etc. All cards are dealt on random (no user input).
I want this to happen in a while loop over and over again and display stats like who won, how many times, what hands were hit etc. So my main() does this:
// GET NUMBER OF PLAYERS
unsigned numOfPls = 1;
Player::getNumOfPls(numOfPls);
// CREATE POINTER TO numOfPls OBJECTS TYPE PLAYER
Player *p[numOfPls];
while(true){
Deck::DeckObj().shuffleDeck();
for (unsigned i=0; i<numOfPls; i++){
p[i] = new Player;
}
// DO I DELETE THIS WITH EACH WHILE ???
for (unsigned i=0; i<numOfPls; i++){
//delete p[i];
}
}
return 0;
I am not sure if I should delete p[i] each time. I'm reading "Sams Teach Yourself C++" where author says I should never reuse deleted pointers. But (not sure here) delete p[i] would not delete pointer, but only object created in array of object and pointed by p+i, correct?
On the other hand (if my above assumptions are correct) is it safe not to delete it and just overwrite object with another object created in another while ? What is the correct approach?
Also I guess it's OK not to delete *p as it will get destroyed with end of main()? (there will be some sort of "break" mechanism applied later).