I have the following classes:
class Thing
{
public:
Thing() {};
virtual ~Thing() {}
};
class MyOwnThing : public Thing
{
public:
std::vector<int> m;
MyOwnThing() {};
void init()
{
m.push_back(0);
m.push_back(0);
m.push_back(0);
m.push_back(0);
m.push_back(0);
m.push_back(0);
m.push_back(0);
m.push_back(0);
m.push_back(0);
m.push_back(0);
m.push_back(0);
puts("done");
}
};
Nothing unusual so far, except for the virtual destructor in the base class. Now, This is what I'm doing in the main function:
int main()
{
MyOwnThing *t = (MyOwnThing *) new Thing(); // (1)
t->init();
delete t;
return 0;
}
The code above generates a nice segfault on the 2nd call to push_back
. Everything runs smoothly if I remove the virtual destructor from Thing
. What's wrong?
1) I suspect that this cast might be the source of the problem.