473,770 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Allocating a local variable to a member function

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

Oct 10 '06 #1
3 1914
Angus wrote:
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
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.

--
Scott McPhillips [VC++ MVP]

Oct 10 '06 #2
On 10 Oct 2006 02:54:42 -0700, "Angus" <an*********@gm ail.comwrote:
>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_Sock et = pSocket;
myagent.m_strL ogin = 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
Oct 10 '06 #3
"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

Oct 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2034
by: Seb | last post by:
Is this efficient for a header file and a class? Does a 'static local' variable get created for each instance or include of the header file? Also, 'return _type().ToString();' does not seem to work. The error states: Object.h(16): error C2662: 'DataLib::Object::_type' : cannot convert 'this' pointer from 'const DataLib::Object' to 'DataLib::Object &' namespace DataLib
18
2953
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
5
1927
by: Henrietta Denoue | last post by:
Hi I am new to C++ and suffering from dynamic allocation. I have a function that returns an integer pointer and want to assign this to another pointer: class cssReader { public:
3
1841
by: Milan Gornik | last post by:
Hello to all, My question is on right way of returning newly created class from a function (and thus, from class method or operator). As I currently see it, there are two different ways to do that. One way is to create new class as a local (automatic) variable in function and then to return it:
5
2178
by: john | last post by:
By doing: void MyClass::MyFunction() { static int myvar; .... } We can defined a function local variable 'myvar' that keeps its value from call to call (the point is that the variable can not be easily
3
4608
by: ishekara | last post by:
Hi, Can you solve the puzzle for me? I have a main.cpp, testinclude.h, and test1.cpp and test2.cpp which include testinclude.h testinclude.h has a class Include defined. I always thought that the compiler generates a copy of class Include one each for test1.cpp and test2.cpp. But however they use the same class definition for both test1.cpp and test2.cpp.
23
4020
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some obscure reason why local functions are not so easy to be dealt with in C++, which I do not yet know.
55
6247
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
8
2272
by: Rahul | last post by:
Hi, I have the following code and i get a compilation error, int main() { class Locale { public: static int c;
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.