- 论坛徽章:
- 0
|
10可用积分
如题,我写了下面这个程序,测试dynamic_cast错误处理的情况
#include <stdio.h>
#include <stdlib.h>
#include <typeinfo>
using namespace std;
class A{
public:
int x;
virtual void fa(){}
};
class B{
public:
int y;
//virtual void foo(){}
};
class D:public B{
public:
int z;
};
int main(int argc, char *argv[]){
B* pb=new B();
B& rb=*pb;
try{
//D& rd =dynamic_cast<D&>(rb);//本语句会抛出std::bad_cast
A* pa=(A*)pb;
D* pd=dynamic_cast<D*>(pa);
}catch(std::bad_cast){
printf("std::bad_cast\n");
}catch(...){
printf("get other exception\n");
}
//system("PAUSE");
return 0;
}
|
我期望pa被dynamic_cast转型的时候,能抛出某种异常,但是实际上程序崩溃了。
=====================================================
我的环境是winxp+devcpp4992,是gcc/gdb的集成环
>gcc --version
gcc (GCC) 3.4.2 (mingw-special)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
请dx帮忙解释一下,10分感谢!
[ 本帖最后由 jeanlove 于 2009-2-1 14:28 编辑 ] |
最佳答案
查看完整内容
我觉得崩溃时因为你前面的那个c-style cast使得RTTI寻找对象的类层次时候访问内存越界导致的。
|