Why does an "extra" pair of copy constructor and destruction occur in code below?
It happens when the constructor of Dingledong takes an STL container as an argument (I've tried std::vector and std::list). It may happen for other things? It doesn't happen if the constructor takes a pointer instead. It also doesn't happen if I allocate ding on heap instead( Dingledong* ding = new Dingledong(v) ).
#include <list>
#include <iostream>
class Dingledong{
public:
Dingledong(std::list<int> numbers)
{
std::cout << "construction\n";
numbers_ = numbers;
}
Dingledong(Dingledong const& other)
{
std::cout << "COPY construction\n";
}
~Dingledong()
{
std::cout << "destructed\n";
// I would do some cleanup here.. unsubscribe from events, whatever..
// but the destructor is called sooner than I would expect and thus
// the cleanup is done prematurely.
}
std::list<int> numbers_;
};
void diller()
{
std::cout << "diller started.. " << std::endl;
std::list<int> v = std::list<int>(34);
// Having an STL container as parameter in constructor causes Dingledong's copy constructor to
// be used to create a temporary Dingledong which is immediately destructed again. Why?
Dingledong ding = Dingledong(v);
std::cout << "Has ding been destructed?\n";
}
int main()
{
diller();
system("pause");
}
Output:
diller started...
construction
COPY construction // I didn't expect this to happen
destructed // I didn't expect this to happen
Has ding been destructed?
destructed
Thank you in advance!