Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just taking this snippet of code, you leak dynamically created cards.</p> <pre><code>Card * c; vector&lt;Card&gt; _shoe; for( int i = 0; i &lt; NUM_CARDS_IN_SHOE; i++ ) { c = new Card(); _shoe.push_back( *c ); } </code></pre> <p><code>_shoe.push_back( *c )</code> adds a <em>copy</em> of the <code>Card</code> object pointed to by <code>c</code> to the vector of <code>Card</code>s. You then fail to delete the original <code>Card</code> as created in the line before.</p> <p>Allocating a vector of <code>NUM_CARDS_IN_SHOE</code> <code>Cards</code> can much more simply be achieved like this:</p> <pre><code>std::vector&lt;Card&gt; _shoe( NUM_CARDS_IN_SHOE ); </code></pre> <p>Looking at your card structure, it looks like you have (or nearly have) strict ownership between objects so I don't think that you need to dynamically create your <code>Card</code>s.</p> <p>Note that your local variable <code>_shoe</code> is shadowing the class variable <code>_shoe</code>. This probably isn't what you want as the local <code>_shoe</code> which you pass to the <code>Deck</code> constructor will go out of scope at the end of the constructor.</p> <p>If you reorder you variables in <code>SolitaireGame</code>, you can probably do something like this:</p> <pre><code>class SolitaireGame: { public: SolitaireGame( int numsuits = 1 ); private: vector&lt;Card&gt; _shoe; Deck _deck; }; SolitaireGame::SolitaireGame( int numsuits ) : _shoe(NUM_CARDS_IN_SHOE) , _deck(_shoe) { } </code></pre> <p>I've changed <code>_deck</code> from being a pointer. I'm using the fact that member variables are constructed in the order declared in the class definition, so <code>_shoe</code> will be fully constructed before it is passed as a reference to the constructor for <code>_deck</code>. The advantage of this is that I have eliminated the need to dynamically allocate <code>_deck</code>. With no uses of <code>new</code>, I know that I can't have any missed calls to <code>delete</code> as nothing needs to be deallocated explicitly.</p> <p>You are right that you can store pointers to the <code>Card</code>s in <code>_shoe</code> in your <code>_deck</code> without any memory management issues, but note that you must not add or remove any of the <code>Card</code>s in the <code>_shoe</code> during the lifetime of the game otherwise you will invalidate all of the pointers in <code>_deck</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload