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;
};