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

Now i know that this one is actually not a very technical question but
I have very long integer sequences that look like this (arbitrary length!): 0000000001110002220033333 Now
Ok this question is not exactly technical but very pertinent and current. If you
This is a very beginner question. I have tried to search for advice but
Hey I'm aware this question isn't very technical, but I'm new to the Delphi
I have very simple OpenGL ES example similar to Hehe's example : http://nehe.gamedev.net/tutorial/ios_lesson_02__first_triangle/50001/ As
I have very little experience working with sockets and multithreaded programming so to learn
I am very new to programming, so I apologize if this question seems absurdly
A rather technical question, but it's still a valid one. I have an app
I'm not sure if this is an appropriate question for StackOverflow. I have a

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.