#include <vector>
#include <iostream>
using namespace std;
class Test
{
public:
Test()
{
cout<<"In TEST constructor"<<endl;
}
~Test()
{
cout<<"In TEST destructor"<<endl;
}
};
int main()
{
Test t1;
vector<Test> vec;
vec.push_back( t1 );
cout<<"About to clear vector"<<endl;
vec.clear();
}
I suspect I'm missing something fundamental. When I run this, I get the cout for the destructor twice. (Why would clearing the vector cause the destructor of Test to be called?)
./Temp
In TEST constructor
About to clear vector
In TEST destructor
In TEST destructor