Destructors are called in reverse order of object creation in C++ but I do not understand why it is not maintained for array of objects.
#include <iostream>
using namespace std;
class test {
int nmbr;
static int c;
public:
test(int j)
{
cout<<"constructor is called for object no :"<<j<<endl;
nmbr=j;
};
~test()
{
c++;
cout<<"destructor is called for object no :"<<c<<endl;
};
};
int test::c=0;
int main()
{
test ob[]={test(1),test(2),test(3)};
return 0;
}
The above program outputs
constructor is called for object no :1
constructor is called for object no :2
constructor is called for object no :3
destructor is called for object no :1
destructor is called for object no :2
destructor is called for object no :3
But why destructors are not called in reverse order?