如果子类有虚的析构函数而父类没有,会崩溃,原因是啥?
isarc 2011-04-18 04:04:24 各位高人好,请你们指教一个问题。
class X
{
public:
X(){ cout << "X class constructor" << endl;}
~X(){ cout << "X class deconstructor " << endl;}
protected:
private:
};
class XSon : public X
{
public:
XSon(){ cout << "XSon class constructor" << endl;}
virtual ~XSon()
{
cout << "XSon class deconstructor " << endl;
}
// virtual void Copy( XSon* obj );
protected:
private:
};
int main(int argc, char* argv[])
{
XSon* sonp = new XSon;
delete sonp;
cout << "--------------------" << endl;
X* xp = new X;
delete xp;
cout << "--------------------" << endl;
X* xp2 = new XSon;
delete xp2;//执行到这里崩溃。。。。。。。。。
}
我google之后,没有看到具体的解答,看到人家的一句话,感觉答案是子类的虚函数表被拷贝给了父类造成了崩溃,但是,what,why我都不知道,请大家帮个忙。
谢谢。