Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 387905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:43:04+00:00 2026-05-12T15:43:04+00:00

I am relatively new to C++ programming, but am a C programmer of 10

  • 0

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.

I’m writing a Solitaire game – is this design unsafe? Is there a better way?

Anyway, I have a class SolitaireGame:

class SolitaireGame:
{
    public:
        SolitaireGame( int numsuits = 1 );
    private:
        Deck * _deck;
        vector<Card> _shoe;
};

The Deck is defined thus:

class Deck:
{
public:
 Deck::Deck( vector<Card>& shoe );
 ~Deck();
 int DealsLeft() const { return deals_left; }
 Card * PullCard();
private:
 int deals_left;
 int num_each_deal;
 deque<Card *> _cards;
};

The Deck constructor, takes a reference to a vector of Card objects ( the shoe, normally 104 cards ) and pushes a pointer to each card onto it’s own deque of pointers.

Deck::Deck( vector<Card>& shoe )
{
    vector<Card>::iterator iter = shoe.begin();

    while( iter != shoe.end() )
    {
        _cards.push_front( &(*iter) );
        iter++;
    }
}

}

The shoe is created in the SolitaireGame constructor. Once this vector of dynamically created Card objects has been created – I then pass a reference to this vector to the constructor.

SolitaireGame::SolitaireGame( int numsuits ):_numsuits(numsuits ) 
{
    Card * c;
    vector<Card> _shoe;

    for( int i = 0; i < NUM_CARDS_IN_SHOE; i++ )
    {
        c = new Card();
        _shoe.push_back( *c );
    }

    _deck = new Deck( _shoe );
}

My idea was that the shoe would be the container for the actual memory for the Card objects and the Deck and Columns just handle pointers to those Card objects.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:43:05+00:00Added an answer on May 12, 2026 at 3:43 pm

    Just taking this snippet of code, you leak dynamically created cards.

    Card * c;
    vector<Card> _shoe;
    
    for( int i = 0; i < NUM_CARDS_IN_SHOE; i++ )
    {
        c = new Card();
        _shoe.push_back( *c );
    }
    

    _shoe.push_back( *c ) adds a copy of the Card object pointed to by c to the vector of Cards. You then fail to delete the original Card as created in the line before.

    Allocating a vector of NUM_CARDS_IN_SHOE Cards can much more simply be achieved like this:

    std::vector<Card> _shoe( NUM_CARDS_IN_SHOE );
    

    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 Cards.

    Note that your local variable _shoe is shadowing the class variable _shoe. This probably isn’t what you want as the local _shoe which you pass to the Deck constructor will go out of scope at the end of the constructor.

    If you reorder you variables in SolitaireGame, you can probably do something like this:

    class SolitaireGame:
    {
    public:
        SolitaireGame( int numsuits = 1 );
    private:
        vector<Card> _shoe;
        Deck _deck;
    };
    
    SolitaireGame::SolitaireGame( int numsuits )
        : _shoe(NUM_CARDS_IN_SHOE)
        , _deck(_shoe)
    {
    }
    

    I’ve changed _deck from being a pointer. I’m using the fact that member variables are constructed in the order declared in the class definition, so _shoe will be fully constructed before it is passed as a reference to the constructor for _deck. The advantage of this is that I have eliminated the need to dynamically allocate _deck. With no uses of new, I know that I can’t have any missed calls to delete as nothing needs to be deallocated explicitly.

    You are right that you can store pointers to the Cards in _shoe in your _deck without any memory management issues, but note that you must not add or remove any of the Cards in the _shoe during the lifetime of the game otherwise you will invalidate all of the pointers in _deck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being relatively new to functional programming, I expend lots of energy wondering is this
I'm relatively new to .NET programming (and OOP in general) and I want to
I'm relatively new to web application programming so I hope this question isn't too
I'm relatively new to Python and am having problems programming with Scapy, the Python
Being relatively new to the .net game, I was wondering, has anyone had any
I'm relatively new to Java programming and need to parse a complex JSON object
Relatively new to Cocoa here. This question is about NSFileHandle, but I got a
I'm relatively new to Haskell with main programming background coming from OO languages. I
Relatively new to rails and trying to model a very simple family tree with
Caveat: I'm relatively new to coding as well as TextMate , so apologies if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.