STL in C++ how does it work
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 strF开发者_JAVA百科irstName;
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
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.
Go here http://cplusplus.com
In the constructor you should use an inizialisation list.
When calling methodes or assign values to existing variables you mustn't write the type. So you always create new variables.
Example:
private:
list<Player> listOfPlayers;
public:
void addPlayer(string firstName, string lastName, int id) {
Player newPlayer(string firstName, string lastName, int id);
}
}
should be
private:
list<Player> listOfPlayers;
public:
void addPlayer(string firstName, string lastName, int id) {
Player newPlayer(firstName, lastName, id);
}
}
I assume you are using the STL string class (std::string).
The string objects are created when Player is created automatically using std::string's default constructor.
When you say: strFirstName = firstName, you are invoking the copy constructor which replaces in memory the object. Try not to think about pointers, C++ is about minimizing them.
So you do not need to use malloc when using strFirstName = "blah".
(Btw I don't know what your talking about with "new account")
Why do you want to allocate memory in your constructor? The posted code has nothing which makes memory allocation a must.
With standard STL containers like list, string etc, you may or may not want to use custom memory allocators. The typical declaration in your case is list<Player>
. If you are inserting Player objects inside the list they will be copy-constructed [so do provide a proper copy-constructor if you are dabbling with pointers inside your class]
If you want custom allocation of memory for your list or string, the declaration will be like list<Player, CustomAllocator>
etc.
The standard provides an allocator that internally uses the global operators that is used as the default one everywhere an allocator is needed. Look into C++ standard, section 20.4.1. The public interface is well described out there.
精彩评论