You have only define a default constructor, but of course a constructor can be called with various parameters too. Since you do not explicitly define a copy constructor, (a constructor that takes a const my_type&
), the compiler implicitly generates one for you. If you add your own copy constructor, you should see that it is used to construct the other two mysterious objects:
struct my_type
{
my_type(){ cout << (size_t)this << " construction"; }
my_type(const my_type&){ cout << (size_t)this << " copy construction"; }
~my_type(){ cout << (size_t)this << " destruction"; }
};
In fact, if you're using a C++11 compiler, an implicitly generated move constructor will be responsible for some of the new objects. If you provide a copy constructor as above, then the implicit move constructor is not generated any more and so they all show up as copy constructions. However, if you provide a move constructor too, you will find it being called:
struct my_type
{
my_type(){ cout << (size_t)this << " construction"; }
my_type(const my_type&){ cout << (size_t)this << " copy construction"; }
my_type(my_type&&){ cout << (size_t)this << " move construction"; }
~my_type(){ cout << (size_t)this << " destruction"; }
};