R
Ravi
Here is the code (also at http://pastebin.com/yw5z2hnG ):
#include <iostream>
#include <vector>
using namespace std;
class X
{
public:
int i;
X();
~X();
};
X::X()
{
i = 1;
cout << "---constructor" << '\n';
}
X::~X()
{
cout << "***desctructor" << '\n';
}
int main()
{
vector<X> *vx = new vector<X>;
cout << "------------------------------------" << endl;
vx->push_back(X());
vx->push_back(X());
vx->push_back(X());
vx->push_back(X());
vx->push_back(X());
cout << "------------------------------------" << endl;
delete vx;
}
I get the output as:
------------------------------------
---constructor
***desctructor
---constructor
***desctructor
***desctructor
---constructor
***desctructor
***desctructor
***desctructor
---constructor
***desctructor
---constructor
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor
------------------------------------
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor
I do not understand why so many destructors are called.
#include <iostream>
#include <vector>
using namespace std;
class X
{
public:
int i;
X();
~X();
};
X::X()
{
i = 1;
cout << "---constructor" << '\n';
}
X::~X()
{
cout << "***desctructor" << '\n';
}
int main()
{
vector<X> *vx = new vector<X>;
cout << "------------------------------------" << endl;
vx->push_back(X());
vx->push_back(X());
vx->push_back(X());
vx->push_back(X());
vx->push_back(X());
cout << "------------------------------------" << endl;
delete vx;
}
I get the output as:
------------------------------------
---constructor
***desctructor
---constructor
***desctructor
***desctructor
---constructor
***desctructor
***desctructor
***desctructor
---constructor
***desctructor
---constructor
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor
------------------------------------
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor
I do not understand why so many destructors are called.