现在的位置: 首页 > 综合 > 正文

C++异常处理catch()中变量析构与构造的奇怪问题 C++异常处理catch()中变量析构与构造的奇怪问题

2017年11月05日 ⁄ 综合 ⁄ 共 2306字 ⁄ 字号 评论关闭
 

C++异常处理catch()中变量析构与构造的奇怪问题

分类: C/C++ 291人阅读 评论(0) 收藏 举报

 一直对C++中的try catch结构非常的疑惑。

编写了如下代码来查看被throw的对象,怎样被catch到的。

  1. // test.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <cstdlib>  
  6. #include <new>  
  7. #include <iostream>    
  8. #include <string>    
  9.   
  10. using namespace std;    
  11.   
  12.   
  13. class object  
  14. {  
  15.  static int i;  
  16.  int locali;  
  17. public:  
  18.  ~object()  
  19.  {  
  20.   cout<<"~object()"<<locali<<endl;  
  21.  }  
  22.  object():locali(0)  
  23.  {  
  24.   i++;  
  25.   locali=i;  
  26.   cout<<"object(): "<<locali<<endl;  
  27.   
  28.  }  
  29.   
  30.  friend ostream& operator<<(ostream& io,object obj)  
  31.  {  
  32.   return io<<"object: "<<obj.locali;  
  33.  }  
  34. protected:  
  35. private:  
  36. };  
  37.   
  38. int object::i=0;  
  39.   
  40. int f()  
  41. {  
  42.  throw object();  
  43.  cout<<"throwed"<<endl;  
  44.  return 1;  
  45. }  
  46.   
  47. int main() {   
  48.  try{  
  49.   f();  
  50.   cout<<"end f()"<<endl;  
  51.  }  
  52.  catch(object& obj)  
  53.  {  
  54.   cout<<obj<<endl;  
  55.  }  
  56.  getchar();  
  57. }     

该程序的结果为:

object(): 1
~object()1
object: 1~object()1

~object()1

很是奇怪啊。

析构函数被调用了三次,构造函数只调用了一次。

并且cout<<obj<<endl;中的endl换行没有打出来。

 

下面这段继承的throw对象程序,输出也有问题:

 

  1. // test.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <cstdlib>  
  6. #include <new>  
  7. #include <iostream>    
  8. #include <string>    
  9.   
  10. using namespace std;    
  11.   
  12.   
  13. class object  
  14. {  
  15. protected:  
  16.     string str;  
  17.     static int i;  
  18.     int locali;  
  19. public:  
  20.     virtual ~object()  
  21.     {  
  22.         cout<<"~object()"<<locali<<endl;  
  23.     }  
  24.     object():locali(0),str("object")  
  25.     {  
  26.         i++;  
  27.         locali=i;  
  28.         cout<<"object(): "<<locali<<endl;  
  29.   
  30.     }  
  31.   
  32.     friend ostream& operator<<(ostream& io,object obj)  
  33.     {  
  34.         return io<<obj.str<<": "<<obj.locali;  
  35.     }  
  36. protected:  
  37. private:  
  38. };  
  39.   
  40. class derived: public object  
  41. {  
  42. public:  
  43.     derived():object()  
  44.     {  
  45.         str="derived";  
  46.         i++;  
  47.         locali=i;  
  48.         cout<<"derived(): "<<locali<<endl;  
  49.     }  
  50.   
  51.     virtual ~derived()  
  52.     {  
  53.         cout<<"~derived()"<<locali<<endl;  
  54.     }  
  55. };  
  56.   
  57. int object::i=0;  
  58.   
  59. int f()  
  60. {  
  61.     throw derived();  
  62.     cout<<"throwed"<<endl;  
  63.     return 1;  
  64. }  
  65.   
  66. int main() {   
  67.     try{  
  68.         f();  
  69.         cout<<"end f()"<<endl;  
  70.     }  
  71.     catch(object& obj)  
  72.     {  
  73.         cout<<obj  
  74.             <<endl;  
  75.     }  
  76.     getchar();  
  77. }     

输出为:

object(): 1
derived(): 2
derived: 2~object()2

~derived()2
~object()2

 

~object()被多执行了一次。非常的奇怪

抱歉!评论已关闭.