Извиняюсь, что-то накатило. Давно не практиковался в C++.
#include <stdio.h>
#include <assert.h>
#include <map>
class myclass
{
public:
int x;
~myclass()
{
printf("0x%08x: destructor\n", (unsigned int)(void*)this);
}
myclass(int x = 0) : x(x)
{
printf("0x%08x: constructor\n", (unsigned int)(void*)this);
}
myclass(const myclass &s) : x(s.x)
{
printf("0x%08x: copy by ref\n", (unsigned int)(void*)this);
}
myclass& operator=(const myclass &s)
{
assert(&s != this);
x = s.x;
printf("0x%08x: assign by ref\n", (unsigned int)(void*)this);
return *this;
}
};
class mystruct
{
public:
std::map <int, myclass> map;
};
int main()
{
mystruct b;
mystruct a;
myclass x(10);
printf("-- set:\n");
b.map[1] = x;
printf("-- copy:\n");
a = b;
printf("-- out:\n");
return 0;
}
0xbffe4a18: constructor
-- set:
0xbffe49e0: constructor
0xbffe49dc: copy by ref
0x08f4e01c: copy by ref
0xbffe49dc: destructor
0xbffe49e0: destructor
0x08f4e01c: assign by ref
-- copy:
0x08f4e03c: copy by ref
-- out:
0xbffe4a18: destructor
0x08f4e03c: destructor
0x08f4e01c: destructor