Here is the code:
#include <vector>
#include <iostream>
class A
{
public:
A() { std::cout << __FUNCTION__ << "\n"; }
~A() { std::cout << __FUNCTION__ << "\n"; }
A& operator=(const A&) { std::cout << __FUNCTION__ << "\n"; return *this;}
};
int main(int argc, char* argv[])
{
std::vector<A> as;
A a;
as.push_back(a);
as.push_back(a);
return 0;
}
And here is the output I got:
A::A
A::~A
A::~A
A::~A
A::~A
I understand that the output of the first line is from the call to the c-tor from when ‘a’ is created. One of the calls to the d-tor’s also belongs to a.
What about the other three calls to A::~A(), where are they coming from?
And why are there more calls to the d-tor than calls to the c-tor?
How does the container clone ‘a’ when it adds copies to its elements?
And finally, is the output implementation-defined or are there are other possible outputs?
To understand what happens, you are missing one method in A :
Then you see the output:
What happen is that, for each push_back the vector allocate a new contiguous array, copy the old content, and destroy it. If you count, the first copy constructor is for the first push_back, the second and third for the next push_back. The first destructor is for the second push_back, the two next for the destruction of the vector, the last one for the destruction of the variable a.
And by the way, this is completely implementation defined, as it is allowed to allocate by chunk, which would prevent quite a few copy/destruction. You can even so the chunks yourself by using
vector::reserve(size_type n).