返回信息流class throwobj
{
int i;
public:
throwobj(int j):i(j){}
int geti(){ return i;}
~throwobj(){cout<<"call me"<<endl;}
};
int divide(int a,int b)
{
if(b==0) throw throwobj(1);
return a/b;
}
int _tmain(int argc, _TCHAR* argv[])
{
throwobj *p=NULL;
try
{ cout<< divide(1,1); }
catch (throwobj &oo)
{ p=&oo; cout<<"exception"<<endl;}
cout<<p->geti()<<endl;
return 0;
}
这段代码,cout<<p->geti()<<endl;这里打印的是1
但是如果把类的析构函数去掉,那么上面那句打印的是0
这是怎么搞的~~晕掉了
平台是VC2008
这是一条镜像帖。来源:北邮人论坛 / cpp / #23946同步于 2009/5/19
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
一个关于析构函数的问题
perfectfan
2009/5/19镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
这个问题应该不在析构函数,p->geti()是在访问一个局部的对象,鬼晓得栈会怎么变
【 在 perfectfan 的大作中提到: 】
: class throwobj
: {
: int i;
: ...................
#include <iostream>
改成这样就没问题了
#include <tchar.h>
using namespace std;
class throwobj
{
int i;
public:
throwobj(int j):i(j){}
int geti(){ return i;}
~throwobj(){cout<<"call me"<<endl;}
};
int divide(int a,int b)
{
if(b==0) throw throwobj(1);
return a/b;
}
int _tmain(int argc, _TCHAR* argv[])
{
throwobj *p=NULL;
try
{ cout<< divide(1,0); }
catch (throwobj &oo)
{ p=&oo; cout<<"exception"<<endl;
cout<<p->geti()<<endl; }
return 0;
}