Throw Exception 中的构造与析构 问题

viterming 2008-12-16 07:56:18
写了个简单函数:程序如下:
1. 如果没有拷贝构造函数,那么输出结果如下:
Enter try...
MyFunc
Expt()
~Expt() - 0 < <<<<<< 这个是怎么出来的啊????????>>>>>>>> >
Expt...
~Expt() - 0
~Expt() - 0

2.如果加上拷贝构造函数,则结果比较正常,如下:

Enter try...
MyFunc
Expt()
copy Expt()...
Expt...
~Expt() - 1
~Expt() - 0

请帮忙解释下第一种结果,不知道欧文哪里理解有问题!!!


// cpp10.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
using namespace std;
class Expt
{
public:
Expt(){
i=0;
cout<<"Expt()"<<endl;
}
~Expt(){
cout<<"~Expt()"<< " - "<<i<<endl;
}
//Expt(Expt &){ i=1; cout<<"copy Expt()..."<<endl; } //拷贝构造函数
const char *ShowReason() const
{
return "Expt...";
}
private:
int i;
};

void MyFunc()
{
cout<<"MyFunc"<<endl;
throw Expt();
}

int _tmain(int argc, _TCHAR* argv[])
{
try
{
cout<<"Enter try..." <<endl;
MyFunc();

}
catch( Expt E )
{
cout<<E.ShowReason()<<endl;
}
return 0;
}

...全文
131 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
viterming 2008-12-18
  • 打赏
  • 举报
回复
结论:

不同的编译器有不同的结果:
1.Solaris, AIX, Linux
MyFunc
Expt() - 1
Message...
~Expt() - 1


2. HP ,Windows
MyFunc
Expt() - 1
~Expt() - 1
Message...
~Expt() - 0

#include <iostream>
using namespace std;
int i=0;
class Expt
{
public:
Expt(){
i++;
cout<<"Expt()"<<" - "<<i<<endl;
}
~Expt(){
cout<<"~Expt()"<< " - "<<i<<endl;i--;
}

// Expt( const Expt & abc){i++;cout<<"Expt() copy..."<<" - "<<i<<endl;}

const char *ShowReason() const
{
return "Message...";
}
};

void MyFunc()
{
cout<<"MyFunc"<<endl;
throw Expt();
}

int main(int argc, char* argv[])
{
try
{
MyFunc();

}
catch( Expt& E )
{
cout<<E.ShowReason()<<endl;
}
return 0;
}

谢谢诶各位。
viterming 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 eatsweetpotato 的回复:]
Enter try...
MyFunc
Expt()
copy Expt()...
Expt...
~Expt() - 1
~Expt() - 0


按LZ的代码看,当自定义复制构造函数后,在执行
throw Expt()与catch( Expt E ) 时都应该调用复制构造函数,但事实上只有catch时才调用复制构造函数

为什么在throw时没有调用复制构造函数
[/Quote]

我的理解是throw Expt();是不调用,但catch( Expt E ) 会调用。。

不过 比较不好理解的是:如果不定义拷贝复制函数的话,会有三次析构,而不是两次?
viterming 2008-12-18
  • 打赏
  • 举报
回复
taodm:

回帖是种美德,不过回之前先看看我的问题。。。。谢谢。
eatsweetpotato 2008-12-17
  • 打赏
  • 举报
回复
Enter try...
MyFunc
Expt()
copy Expt()...
Expt...
~Expt() - 1
~Expt() - 0


按LZ的代码看,当自定义复制构造函数后,在执行
throw Expt()与catch( Expt E ) 时都应该调用复制构造函数,但事实上只有catch时才调用复制构造函数

为什么在throw时没有调用复制构造函数
taodm 2008-12-17
  • 打赏
  • 举报
回复
楼主,听说过编译器会自动生成拷贝构造函数没有?
eatsweetpotato 2008-12-17
  • 打赏
  • 举报
回复
class Expt
{
public:
Expt( )
{
i=0;
cout <<"Expt()" <<endl;
}

Expt(Expt &o)
{
i=1;
cout <<"copy Expt()..." <<endl;
}

/*
*/
~Expt()
{
cout <<"~Expt()" << " - " <<i <<endl;
}
Expt & operator=(Expt&one)
{
cout << "operator ="<<endl;
}

const char *ShowReason() const
{
return "Expt...";
}

private:
int i;
};

void MyFunc()
{
cout <<"MyFunc" <<endl;
throw Expt();
}

int main()
{
try
{
cout <<"Enter try..." <<endl;
MyFunc();

}
catch ( Expt &E )
{
cout <<E.ShowReason() <<endl;
}
return 0;
}

奇怪,我在vc05上运行时居然没有调用复制构造函数,按道理在执行throw时应该调用复制的
eatsweetpotato 2008-12-17
  • 打赏
  • 举报
回复

class Expt
{
public:
Expt( )
{
i=0;
cout <<"Expt()" <<endl;
}

Expt(Expt &o)
{
i=1;
cout <<"copy Expt()..." <<endl;
}

/*
*/
~Expt()
{
cout <<"~Expt()" << " - " <<i <<endl;
}
Expt & operator=(Expt&one)
{
cout << "operator ="<<endl;
}

const char *ShowReason() const
{
return "Expt...";
}

private:
int i;
};

void MyFunc()
{
cout <<"MyFunc" <<endl;
throw Expt();
}

int main()
{
try
{
cout <<"Enter try..." <<endl;
MyFunc();

}
catch ( Expt &E )
{
cout <<E.ShowReason() <<endl;
}
return 0;
}


奇怪,我在vc05上运行时居然没有调用复制构造函数,按道理在执行throw时应该调用复制的
herman~~ 2008-12-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hqin6 的回复:]
C/C++ codecatch( Expt& E )

如果是catch( Expt E ) ,会调用复制构造函数。产生的一个临时对象析构造成的!
[/Quote]

同意
viterming 2008-12-16
  • 打赏
  • 举报
回复
谢谢两位。

我的问题是:

为什么有拷贝构造函数的时候,构造析构两次,其中一次构造,另一次拷贝构造,合理!

但是没有拷贝构造函数的时候,构造一次,析构三次,多出一次构造也析构,不能理解!!!希望路过的帮忙解答,谢谢!
eatsweetpotato 2008-12-16
  • 打赏
  • 举报
回复
个人分析如下:
void MyFunc()
{
cout < <"MyFunc" < <endl;
throw Expt(); //产生一个自动变量,调用复制构造函数后这里的自动变量被析构
}

catch( Expt& E ) //此时引用的是刚才通过复制构造函数产生的对象
catch( Expt E ) //如果是这种情况的话,又得调用复制构造函数来复制刚才通过复制构造函数产生的对象

以上个人观点。
太乙 2008-12-16
  • 打赏
  • 举报
回复
catch( Expt& E ) 

如果是catch( Expt E ) ,会调用复制构造函数。产生的一个临时对象析构造成的!

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧