鱼C论坛

 找回密码
 立即注册
查看: 1970|回复: 1

小白求助 关于异常对象的生命周期

[复制链接]
发表于 2020-5-28 11:17:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include "iostream"
  2. using std::endl;
  3. using std::cout;
  4. using std::cin;

  5. class BadSrcType{};
  6. class BadDestType{};
  7. class BadProcessType
  8. {
  9. public:
  10.         BadProcessType()
  11.         {
  12.                 cout << "BadProcessType构造执行了\n";
  13.         }
  14.         BadProcessType(const BadProcessType& bp)
  15.         {
  16.                 cout << "BadProcessType copy 构造执行了\n";
  17.         }
  18.         ~BadProcessType()
  19.         {
  20.                 cout << "~BadProcessType析构执行了\n";
  21.         }
  22. };

  23. void my_strcpy(char* from, char* to)
  24. {
  25.         if (from == nullptr)
  26.         {
  27.                 throw BadSrcType();
  28.         }
  29.         if (to == nullptr)
  30.         {
  31.                 throw BadDestType();
  32.         }
  33.         if (*from == 'a')
  34.         {
  35.                 throw BadProcessType();
  36.         }
  37.         if (*from == 'b')
  38.         {
  39.                 throw& (BadProcessType());
  40.         }
  41. }

  42. void main()
  43. {
  44.         char buf1[] = "bacdefg";
  45.         char buf2[8];

  46.         try
  47.         {
  48.                 my_strcpy(buf1, buf2);
  49.         }
  50.         catch (BadProcessType e)  //问题1:当这个catch捕捉到异常的时候 为啥catch运行完了 异常对象和e对象都析构?
  51.         {
  52.                 cout << "BadProcessTypece类型错误\n";
  53.         }
  54.         catch (BadProcessType * e)//问题2:为啥在这个catch捕捉到异常之前 异常对象已经析构了?
  55.         {
  56.                 cout << "BadProcessTypece*类型错误\n";
  57.         }
  58.         system("pause");
  59. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-28 11:38:59 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 15:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表