When you say myObjectsMap [0], you call the default constructor for MyObject. This is because there is nothing in [0], and you just turned to it. This is in the manual .
When you hit MyObject (); you create a temporary instance of MyObject using the default constructor.
Since you allowed the compiler to define your copy constructor, you will see more destructors than constructor messages. (Unlike a house building, there can only be one destructor, but many constructors.) If you do not need to copy this object, then you probably want to declare a private copy constructor and copy operator.
You call the default constructor and copy constructor twice each using this code:
myObjectsMap[0] = MyObject();
When you do this:
myObjectsMap.insert( MyObjectPair(0,MyObject()));
you call the default constructor once and the copy constructor 3 times.
You should probably use pointers as map values instead of the objects themselves, in particular, I suggest looking at shared_ptr.
note: tests were done using GCC 3.4.5 on a Windows NT 5.1 machine.
Terryp
source share