![]() |
|
#1
|
|||
|
|||
Keeping track of a private variable outside the classHi,
I am making a Gin Rummy (card game) implementation in C++. I have created the deck of cards, shuffled it and now I need to deal the cards to the players. Inside the DeckOfCards function I have a private integer variable that is supposed to keep track of the current card in the deck. I instansiate 4 players and 1 deck in the main(). The problem comes when the deal function of the deckOfCards is called as the currentcard variable is resetted for each player, and thus all the players get the same cards. Any clue how I could keep track of the currentcard so that it is not reset every time the DeckOfCards class is called? Thanx for any answers in advance! |
|
#2
|
|||
|
|||
|
Hi Viper,
I am somewhat new to C++ programming but I think I know the solution. If you are using a class then you can define a static data member in the class to keep track of the current card. There will be only one copy So in your class definition CPP / C++ / C Code:
Then in the member-function definition section you'd put int DeckOfCards::currentCard = 0; or whatever you want to initialize it as. From your problem statement however I suspect there may be other issues because with only 1 deck in Main there should not be multiple copies of the currentcard variable being instantiated. Hope that helps, Ian |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Having a problem | Chuckles | Computer Hardware Forum | 19 | 13-Sep-2004 12:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The