Note that there are some explanatory texts on larger screens.

plurals
  1. POIs the use of vectors of pointers here unnecessary or worse, cause memory leaks?
    primarykey
    data
    text
    <p>I am relatively new to C++ programming, but am a C programmer of 10 years so am more comfortable with pointers to objects than I am with references to objects. </p> <p>I'm writing a Solitaire game - is this design unsafe? Is there a better way?</p> <p>Anyway, I have a class <code>SolitaireGame</code>:</p> <pre><code>class SolitaireGame: { public: SolitaireGame( int numsuits = 1 ); private: Deck * _deck; vector&lt;Card&gt; _shoe; }; </code></pre> <p>The <code>Deck</code> is defined thus:</p> <pre><code>class Deck: { public: Deck::Deck( vector&lt;Card&gt;&amp; shoe ); ~Deck(); int DealsLeft() const { return deals_left; } Card * PullCard(); private: int deals_left; int num_each_deal; deque&lt;Card *&gt; _cards; }; </code></pre> <p>The <code>Deck</code> constructor, takes a reference to a vector of <code>Card</code> objects ( the shoe, normally 104 cards ) and pushes a pointer to each card onto it's own deque of pointers.</p> <pre><code>Deck::Deck( vector&lt;Card&gt;&amp; shoe ) { vector&lt;Card&gt;::iterator iter = shoe.begin(); while( iter != shoe.end() ) { _cards.push_front( &amp;(*iter) ); iter++; } } </code></pre> <p>}</p> <p>The shoe is created in the <code>SolitaireGame</code> constructor. Once this vector of dynamically created <code>Card</code> objects has been created - I then pass a reference to this vector to the constructor.</p> <pre><code>SolitaireGame::SolitaireGame( int numsuits ):_numsuits(numsuits ) { 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 ); } _deck = new Deck( _shoe ); } </code></pre> <p>My idea was that the shoe would be the container for the actual memory for the <code>Card</code> objects and the <code>Deck</code> and <code>Columns</code> just handle pointers to those <code>Card</code> objects.</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.
 

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