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?