Allocating a local variable to a member function

A

Angus

Hello

I have a member variable: std::map<int, CAgents> m_AgentsList;

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_Socket = pSocket;
myagent.m_strLogin = 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
 
S

Scott McPhillips [MVP]

Angus said:
Hello

I have a member variable: std::map<int, CAgents> m_AgentsList;

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_Socket = pSocket;
myagent.m_strLogin = 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

The data is copied to m_AgentsList, so it does not matter that the local
variable is later destroyed. Copying the pSocket pointer does not make
a copy of the socket object. The original socket remains, and the copy
of the pointer can be used to access it.
 
R

Roland Pibinger

I have a member variable: std::map<int, CAgents> m_AgentsList;

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_Socket = pSocket;
myagent.m_strLogin = strLogin;
m_AgentsList[dwDevice] = myagent;

pSocket is a pointer to the client socket.

So m_Socket is also a pointer.
Basically I need to make a copy of the client socket.

You don't do it and you probably need not do it. You copy a pointer to
a client socket object, not the client socket object.
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?

You should clarify the ownership of the (probably dynamcally allcated)
client socket. Who is supposed to delete it?

Best wishes,
Roland Pibinger
 
J

Jim Langston

Angus said:
Hello

I have a member variable: std::map<int, CAgents> m_AgentsList;

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_Socket = pSocket;
myagent.m_strLogin = 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::iterator it = World.ConnectedPlayers.find( Socket );
if ( it != World.ConnectedPlayers.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.ConnectedPlayers[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top