"Angus" <an*********@gm ail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Hello
I have a member variable: std::map<int, CAgentsm_Agents List;
CAgents is just a really small class with some member variables. Just
really a container for agent data.
Agents log in to a server. In my login function I do this:
CAgents myagent;
myagent.m_Socke t = pSocket;
myagent.m_strLo gin = strLogin;
m_AgentsList[dwDevice] = myagent;
pSocket is a pointer to the client socket.
Basically I need to make a copy of the client socket. But I am
allocating a local variable to a member variable. When the function
returns myagent is no more. Will this not matter because the data is
copied to m_AgentsList? What about pSocket?
Angus
pSocket is an int, the socket number. When you get communications from this
client, you should in some function also get the socket number, so no need
to save it outside the map. And you can always get to the CAgents data via
the socket number (or iterating over the map).
So, you are right that "this not matter because the data is copied to
m_AgentsList".
What I am doing, and maybe you should, maybe you shouldn't, what I"m doing
not be the best but it works for me, is I have a function:
CPlayer& FindPlayer( const SOCKET Socket )
{
// Get a reference in the map for this player
map_player::ite rator it = World.Connected Players.find( Socket );
if ( it != World.Connected Players.end() )
return (*it).second;
else
throw 0;
}
map_player is just a typedef so I don't have to keep typing out the map
info:
typedef std::map< SOCKET, CPlayer map_player;
Even if you don't use this exact code, you should understand what it's
doing. The reason I don't simply do:
return World.Connected Players[Socket];
is because if that socket is inot in my map, that would acutally add it,
which I don't want, so I do the find with the throw. You may find other
options better for you.
HTH