首页 > 解决方案 > 返回被删除节点的值

问题描述

int dequeueAndReturnValue()
{
    if (front==NULL)
        cout << "There is no items to vomit" << endl;
    else
    {
        Node*ptr = front;
        front = front->next;
        delete ptr;
    }
}

我想从一个名为 int 的方法中返回已删除指针的值

标签: c++

解决方案


这应该有效:

 std::unique_ptr<Node> ptr( front );
 front = ptr->next;
 return ptr->value;

但是您实际上应该首先使用智能指针。

注意:如果front等于nullptr(并且您应该使用它而不是NULL),您也必须返回某些内容或抛出异常,否则您的代码将具有未定义的行为。


推荐阅读