STL::map, how to prevent use of temporary object?
-
Hi, everyone. Sure, this is a dummy question, but I'm newbie with STL, so please, don't be angry. :) The problem (or maybe feature) is in stl::map.insert(stl::map::value_type(int, myClassObj1)). In this procedure I can see: 1. myClassObj2 is created by copy constructor from myClassObj1 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj2 is destroyed. I guess, myClassObj2 is created in pair(const _T1& _V1, const _T2& _V2) and myClassObj3 is created in map::insert itself. I don't get it. :confused: Could you please explain to me, why temporary object created? And is it possible to get rid of it? Thank you in advance, va'Lery
-
Hi, everyone. Sure, this is a dummy question, but I'm newbie with STL, so please, don't be angry. :) The problem (or maybe feature) is in stl::map.insert(stl::map::value_type(int, myClassObj1)). In this procedure I can see: 1. myClassObj2 is created by copy constructor from myClassObj1 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj2 is destroyed. I guess, myClassObj2 is created in pair(const _T1& _V1, const _T2& _V2) and myClassObj3 is created in map::insert itself. I don't get it. :confused: Could you please explain to me, why temporary object created? And is it possible to get rid of it? Thank you in advance, va'Lery
You need to pass it a pair. e.g.
map<long, CString> mapNames;
// Insert an item :
mapNames.insert(make_pair(5, "Hello"))
Maps are a bit like Hashes, so the first value is the key, and the second is the value to be returned. You may need to define a copy constructor for you class in the pair, depending on what it contains. e.g. pointers and the like. hope this helps, Giles
-
Hi, everyone. Sure, this is a dummy question, but I'm newbie with STL, so please, don't be angry. :) The problem (or maybe feature) is in stl::map.insert(stl::map::value_type(int, myClassObj1)). In this procedure I can see: 1. myClassObj2 is created by copy constructor from myClassObj1 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj2 is destroyed. I guess, myClassObj2 is created in pair(const _T1& _V1, const _T2& _V2) and myClassObj3 is created in map::insert itself. I don't get it. :confused: Could you please explain to me, why temporary object created? And is it possible to get rid of it? Thank you in advance, va'Lery
The temporary object
myClassObj2
is created as part of thepair
passed tomap::insert
, as you correctly guessed. Thispair
is used then internally to recreate the object to be inserted. Unfortunately, the value passed tomap::insert
cannot be used directly for two reasons:- What is passed is a
const
reference, with which little can be done except replicate it. - The object actually inserted into the map is allocated by special means (namely resorting to an STL allocator), so the
pair
you just passed simply does not fit.
I hope this helped you understand the problem. In short, you can't get rid of the temporary object in any easy way. If it is very important not to do excessive copying you should go to some approach using ref-counted pointers or something like that. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
- What is passed is a
-
Hi, everyone. Sure, this is a dummy question, but I'm newbie with STL, so please, don't be angry. :) The problem (or maybe feature) is in stl::map.insert(stl::map::value_type(int, myClassObj1)). In this procedure I can see: 1. myClassObj2 is created by copy constructor from myClassObj1 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj2 is destroyed. I guess, myClassObj2 is created in pair(const _T1& _V1, const _T2& _V2) and myClassObj3 is created in map::insert itself. I don't get it. :confused: Could you please explain to me, why temporary object created? And is it possible to get rid of it? Thank you in advance, va'Lery
You can insert into a map using map[key] = value. Check out my STL article series, part D for an article on the workings of set and map. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
-
You can insert into a map using map[key] = value. Check out my STL article series, part D for an article on the workings of set and map. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
If I use map[key] = myClassObj1 then result is even more weird. 1. myClassObj2 is created by default constructor (constructor without arguments) 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj4 is created by copy constructor from myClassObj3 4. myClassObj3 is destroyed 5. myClassObj2 is destroyed 6. myClassObj4 call operator= myClassObj1 Hmmm... It's too much for me... :) As Joaquín mentioned, there isn't simple way to prevent temporary object. But I have to, because I have collection of collections, so duplication of object will cost me too much. I think there is only one way to do it and it is creation of my own container. Anyway, thanks a lot for all your answers, gents. Life is good but it's better to live good.
-
If I use map[key] = myClassObj1 then result is even more weird. 1. myClassObj2 is created by default constructor (constructor without arguments) 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj4 is created by copy constructor from myClassObj3 4. myClassObj3 is destroyed 5. myClassObj2 is destroyed 6. myClassObj4 call operator= myClassObj1 Hmmm... It's too much for me... :) As Joaquín mentioned, there isn't simple way to prevent temporary object. But I have to, because I have collection of collections, so duplication of object will cost me too much. I think there is only one way to do it and it is creation of my own container. Anyway, thanks a lot for all your answers, gents. Life is good but it's better to live good.
think there is only one way to do it and it is creation of my own container. I'd think twice before going into writing your home made container. Why not have containers of smart pointers to containers? Check Boost
shared_ptr
class template, which automagically gives you ref-counted access to dynamically allocated objects. The hassle of inserting this into your current code should be minimal. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo