I am having the problem that my class destructor is called when the class is constructed. Consider the following test program:
#include <iostream>
#include <vector>
using namespace std;
class X
{
public:
X() { cout << "X::X()" << endl; };
~X() { cout << "X::~X()" << endl; };
};
class Y : public X
{
public:
Y() : X() { cout << "Y::Y()" << endl; };
~Y() { cout << "Y::~Y()" << endl; };
};
int main() {
vector<Y> a;
a.resize(10);
while(true) ;
return 0;
}
The output (from before the loop) is
X::X()
Y::Y()
Y::~Y()
X::~X()
I don't understand the behaviour of the above snippet:
- Why is only a single element constructed?
- Why are the destructors called?