-3

我有以下功能,我的问题是我无法删除 catch 中的 temp 因为它说 temp 未声明但我不明白为什么?任何帮助表示赞赏。

List_Node*List::copy(const List_Node*  list) 
{
    if(list == nullptr) 
    {
        return nullptr;
    }
    else
    {
        try
        {
            List_Node* temp = new List_Node(list -> value_);
            temp -> next_ = copy(list -> next_);
            return temp;
        }
    catch (bad_alloc& )
    {
      delete temp;   
        throw;
    }
 }

}
4

1 回答 1

0

决策可以是这样的:

List_Node*List::copy(const List_Node*  list) 
{
    if(list == nullptr) 
    {
        return nullptr;
    }
    else
    {
        List_Node* temp = 0;
        try
        {
            temp = new List_Node(list -> value_);
            temp -> next_ = copy(list -> next_);
            return temp;
        }
    catch (bad_alloc& )
    {
      delete temp;   
        throw;
    }
 }

}

或者在第一种情况下使用 auto-ptr。

于 2013-06-06T18:19:33.223 回答