几行程序,delete父类指针,导致崩溃,为什么?
我用VC2005新建一个控制台工程,什么编译选项也没有改:
class father{ int i;public: void f(){printf("father\n");} virtual ~father(){}//我已经把父类的dtor指定为虚函数了呀};class child{public: virtual void f(){printf("child\n");}}; int _tmain(int argc, _TCHAR* argv[]){ father* pf=(father*)(new child); child* pc=dynamic_cast<child*>(pf);//如果我想关掉/GR,VC2005/2010用什么编译选项? delete pf;//导致运行时错误,为什么? return 0;}#include "stdafx.h"class father{ int i;public: void f(){printf("father\n");} virtual ~father(){}//我已经把父类的dtor指定为虚函数了呀};class child:public father//child继承father类{public: virtual void f(){printf("child\n");}}; int _tmain(int argc, _TCHAR* argv[]){ father* pf=(father*)(new child); pf->f(); child* pc=dynamic_cast<child*>(pf);//如果我想关掉/GR,VC2005/2010用什么编译选项? pc->f(); delete pf;//导致运行时错误,为什么? return 0;}