Sign in to follow this  
baker

question about inserting obj's in a map, should i declare new first?

Recommended Posts

baker    158
or just declare the obj locally in the function? for example updateOrAddData( * incomingData) { myObj testObj = new myObj; testObj.setData1(incomingData->getData1); testObj.setData2(incomingData->getData2); ...etc myLocStore->myMap.insert(incomingData->getID, testObj) //myLocStore is a ptr to my map object myMap delete testObj } vs.../////////////////////////////////////////////////////////////////// updateOrAddData( * incomingData) { myObj testObj; testObj.setData1(incomingData->getData1); testObj.setData2(incomingData->getData2); ...etc myLocStore->myMap.insert(incomingData->getID, testObj) //myLocStore is a ptr to my map object myMap } is there even a difference? assuming that all i want to do is update or add a new item in my map object myMap? Thanks [Edited by - baker on March 17, 2006 1:01:49 PM]

Share this post


Link to post
Share on other sites
Supernat02    604
What language are you using? I don't understand why you do a new testObj but then delete contactObj. If I understand your question, inserting an object into a map should be fine, but you may have to define the = operator for it. The map does not store the object's address. It only copies what's in the object.

Share this post


Link to post
Share on other sites
baker    158
hi, thanks for the reply. it was a typo in the first part.

my question is, is there a difference if i declare my temp storage as a new vs not-new when populating it before i insert it in my map?

Share this post


Link to post
Share on other sites
Tonin    148
I'm assuming this is C++ and myMap is a std::map.

If myMap holds myObj and not pointers to myObj then there isn't a difference in the result, since std::map makes a copy of the object when it is inserted. I think the version using new and delete will be a bit slower (and more complicated) so I'd go with a simple local object.

I'm no expert (so please don't kill me if I'm wrong!), but hopefully that helps answer your question.
~Andy

PS: If this is C++ there are a bunch of other errors in it, so I'm assuming it's just sudo-code.

Share this post


Link to post
Share on other sites
Kylotan    10010
I just want to confirm that Tonin's 2nd paragraph is entirely right. There is also a big difference in that pointers to objects and objects are not interchangeable so only one of those approaches will work with your data structure (and obviously the pointer version will require the -> operator rather than the . operator that you've used in the example).

Share this post


Link to post
Share on other sites
baker    158
hi...yea, my code was just peusdo code. i should of stated that.

so since the map makes copies of the inserting objects, the temp object should just be declared. if my data object was somthing else that didnt make copies of the inserting object, thats when i would use new.

thanks for responding guys.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this