您的位置:首页 > 其它

如果构造函数出错,如何处理?

2014-04-28 18:01 120 查看
//   构造函数失败处理  
  #include   <iostream>  
  using   namespace   std;  
  class   CFoo   {  
  public:  
     CFoo() 
     {  
            cout   <<   "construct   CFoo."   <<   endl;  
            //   an   error   occurred  
            throw   -1;  
     }  
     ~CFoo()  
    {  
         cout   <<   "destruct   CFoo."   <<   endl;  
    }  
  };  
 
  int   main   (int   argc,   char   *argv[])  
  {  
          CFoo   *Foo   =   NULL;  
          try  
          {  
                  Foo   =   new   CFoo;  
          }  
          catch   (...)  
          {  
                  cout   <<   "catch   exception."   <<   endl;  
                  //   Foo   ==   NULL,   can't   delete   it  
          }  
          getch();  
          return   0;  
  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐