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

  • Home
  • SEARCH
  • 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 861009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:54:32+00:00 2026-05-15T08:54:32+00:00

I have very technical question, I was working with C, and now I’m studying

  • 0

I have very technical question, I was working with C, and now I’m studying C++,
if I have for example this class

class Team {
private:
   list<Player> listOfPlayers;
public:
    void addPlayer(string firstName, string lastName, int id) {
        Player newPlayer(string firstName, string lastName, int id);
            listOfPlayers.push_back(Player(string firstName, string lastName, int id));
    }
};

this is a declaration of the Player:

class Player{
private:
    string strLastName;      
    string strFirstName;     
    int nID;                
public:
    Player(string firstName, string lastName, int id);

};

and this is my constructor of Player:

Player::Player(string firstName, string lastName, int id){
    nId = id;
    string strFirstName = firstName;
    string strLastName = lastName;
}

so my question is when I call function addPlayer what exactly is going on with program,

in my constructor of Account do I need to allocate new memory for new Player(cause in C I always use malloc) for strFirstName and strLastName, or constructor of string of Account and STL do it without me, thanks in advance (if you don’t want to answer my question please at least give me some link with information) thanks in advance

  • 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-15T08:54:33+00:00Added an answer on May 15, 2026 at 8:54 am

    Here’s a “correct” implementation of what you have now:

    #include <list>
    #include <string>
    
    class Player
    { 
    private: 
        std::string strLastName;       
        std::string strFirstName;      
        int nID;                 
    public:
        // You should pass std::strings to functions by const reference.
        // (see Kirill V. Lyadvinsky's comment to OP's question)
        Player(const std::string& firstName, const std::string& lastName, int id); 
    };
    
    // What follows after the colon is called the initializer list.
    Player::Player(const std::string& firstName, const std::string& lastName, int id)
        : strFirstName(firstName), strLastName(lastName), nID(id) {}
    
    class Team
    { 
    private: 
        std::list<Player> listOfPlayers; 
    public: 
        void addPlayer(const std::string& firstName,
            const std::string& lastName, int id)
        { 
            // Constructs a Player instance and adds it to the list.
            listOfPlayers.push_back(Player(firstName, lastName, id)); 
        } 
    };
    

    The push_back() function of list allocates a new node that holds the Player instance and pointers to other nodes, so you have sort of a “chain” of Player instances.

    For your question about Account, if you have this:

    class Account
    {
    private:
        std::string strFirstName;
        std::string strLastName;
    };
    

    Then you don’t need to worry about allocating/freeing memory for the character arrays, for std::string will handle that for you.

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

Sidebar

Related Questions

Not very technical, but... I have to implement a bad words filter in a
I have very simple select like this: SELECT * FROM table WHERE column1 IN
I'm not sure if this is an appropriate question for StackOverflow. I have a
I have an inbound email handler that's working. However, right now I'm worried about
We have very strange problem, one of our applications is continually querying server by
I have very little experience building software for Windows, and zero experience using the
i have very simple problem. I need to create model, that represent element of
I have a very strange problem. Under some elusive circumstances I fail to apply
I have a very simple problem which requires a very quick and simple solution
I have a very simple WPF application in which I am using data binding

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.