For checking the order of destruction of user defined object in stl container, I have tested following code
class A
{
public:
static int i;
const int j;
A():j(++i)
{
cout<<"A::A() "<< j <<endl;
}
~A()
{
cout<<"A::~A(): "<< j <<endl;
}
A(const A & obj):j(++i)
{
cout<<"copyconstro " << j <<endl;
}
};
int A::i = 0;
int main()
{
vector<A> obj;
A ab; //1
A ad;// 2
A c;//3
obj.push_back(ab); //4
vector<A> obj2;
obj2.push_back(c); //5
}
output:
A::A() 1
A::A() 2
A::A() 3
copyconstro 4
copyconstro 5
A::~A(): 5
A::~A(): 3
A::~A(): 2
A::~A(): 1
A::~A(): 4
I am little confused after looking at output.
Does ISO C++ standard mandate any sort of destruction order of objects inside STL containers?