Your code as it stands is very confusing to me, but that may just be because it's midnight where I am.
At the moment, your player class contains a list of players. What that means is that every player has its own list of players associated with it. Is that what you intend? It seems unlikely to me.
The implications of the first scenario mean that every time your
while loop runs, the following things happen:
-- Create a new Player instance and assign it to
entity (overwritting what was there previously -- memory leak!)
-- Set the fields of the Player instance.
-- Add the player instance to it's own list of player instances. This part makes little-to-no sense in my eyes.
It is for more likely that you want something like this:
#include <iostream>
#include <list>
#include <string>
class Player {
private:
int id_;
std::string name_;
public:
explicit Player( int id, const std::string &name ) :
id_( id ),
name_( name ) {}
int id() const { return id_; }
const std::string &name() const { return name_; }
};
typedef std::list<Player> PlayerList;
int main() {
PlayerList plist;
for ( int i = 0; i < 10; i++ )
plist.push_back( Player( i, "Bobby" ) );
}
While I'd normally be very happy to explain
everything in this example, I'll leave it to others since I need to go to sleep. What I
will say is that you need to keep reading/learning: as far as I'm concerned, your code is gibberish.
