I have created some C++ classes to model a Solitaire game as a learning exercise.
I have classes for the SolitaireGame, a CardStack (one of the 10 piles of cards on the board ) and a Card. My current model states that the SolitaireGame owns a vector of 104 Card objects – which I call the ‘shoe’. The SolitaireGame also keeps track of 10 CardStacks which are essentially deque’s of addresses of Card objects stored in the shoe. The Deck and Hand inherit from CardStack. I pass the cards from Deck, to Hand to Cascade by means of pointers to original objects stored in the Shoe.
According to a number of answers I received to this question, I should not be passing the Card’s around by pointers, but should be using const references. The reason being that objects stored in vectors can have their addresses moved about, so storing their addresses anywhere is a no-no. I recently started looking at boost::sharedptr. What do people think about using shared_ptr to Card here?
Here are simplified versions of the classes:
class SolitaireGame
{
public:
SolitaireGame::SolitaireGame( int numsuits );
private:
vector<Card> _shoe;
Deck _deck;
Hand _hand;
CardStack _cols[NUM_COLUMNS];
int _numsuits;
GameState gamestate;
};
class CardStack
{
public:
CardStack(){ cout << "CardStack constructor" << endl; }
CardStack( const CardStack& );
CardStack( const deque<Card *> &d );
~CardStack(){ }
virtual Card * PullCard( Face f );
virtual void PushCard( Card * c );
Card * CardAt( int i ) const;
Card * Top() const;
deque<Card *>::iterator Begin() { return _cards.begin(); }
deque<Card *>::iterator End() { return _cards.end(); }
int Size() const;
CardStack& operator=( const CardStack& rhs );
friend std::ostream& operator<<(std::ostream &os, const CardStack &obj);
private:
deque<Card *> _cards;
};
Storing (const) references is just as bad as storing pointers for the same reason. If the size of the vector does not change as long as other objects hold pointers to the objects therein, you should be safe.
When programming in C++, you should always decide who “owns” an object, e.g. who is responsible to delete it when it is no longer needed. If there is no natural object owner, you could resort to smart pointers like
boost::shared_ptrthat use reference counting or garbage collection to manage the object’s lifetime.In your case, it is pretty obvious that the
SolitaryGameinstance owns all cards. Moreover, the number of cards in the game is fixed. Therefore you can easily pass pointers of your cards to objects that are dependent of the game instance.Once the game is deleted, all cards will get deleted and remaining pointers will be invalid, but at this time, other objects holding card pointers should get deleted, too.