Jump to content
  • Advertisement
Sign in to follow this  
comfortablynumb84

Question about objects

This topic is 4077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone! I'm reading the last chapter of my book "Beginning C++ game programming". It has as a final project a blackjack game. In one of its classes it has a method that puts in a vector the players who will play the game. And here it is:
class Game
{
    private:
        Deck m_Deck;
        vector<Human> m_Players;
        House m_House;
    
    public:
        Game( const vector<string> & names );
        ~Game();
        
        void Play();
};

Game::Game( const vector<string> & names )
{
    vector<string>::const_iterator pIter;
    
    for ( pIter = names.begin() ; pIter < names.end() ; pIter++ )
    {
        m_Players.push_back( Human( *pIter ) );
    }
    
    srand( time( NULL ) );
    m_Deck.Populate();
    m_Deck.Shuffle();
}

Deck, Human and House are other classes. Human's constructor looks like this: Human( const string & name = "" ) : GenericPlayer( name ) {}; GenericPlayer is the base class of Human and House. GenericPlayer's constructor simply assigns name to his data member "m_Name". So.. my question is in this line: m_Players.push_back( Human( *pIter ) ); Why is passed "Human( *pIter )" as an argument of push_back? an object shouldn't be created like this? Human player1( *pIter ); Human player2( *pIter ); .. and so on. What I'm trying to say is.. Can you create an object simply using the constructor? then.. how could you call the object if you don't specify an identifier? Hope you understand why I'm trying to ask. Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
The Human(...) expression creates an anonymous temporary object, which is immediately passed to the push_back function.

Share this post


Link to post
Share on other sites
Thanks for your fast response!

But.. if it's a temporary object, can it be added to a vector? then how would be named that object in the vector? or it hasn't to be named because you can access each object through an iterator?

Share this post


Link to post
Share on other sites
Quote:
Original post by comfortablynumb84
But.. if it's a temporary object, can it be added to a vector? then how would be named that object in the vector? or it hasn't to be named because you can access each object through an iterator?


In C++, objects don't have names, only variables do: variables are bindings between a name and an object, but many objects can exist without a name.

Besides, the vector contains a copy of the passed object anyway, so the fact that it's temporary doesn't change anything, because it's the copy that stays around, not the original object.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!