c++ - 返回被删除节点的值
问题描述
int dequeueAndReturnValue()
{
if (front==NULL)
cout << "There is no items to vomit" << endl;
else
{
Node*ptr = front;
front = front->next;
delete ptr;
}
}
我想从一个名为 int 的方法中返回已删除指针的值
解决方案
这应该有效:
std::unique_ptr<Node> ptr( front );
front = ptr->next;
return ptr->value;
但是您实际上应该首先使用智能指针。
注意:如果front
等于nullptr
(并且您应该使用它而不是NULL
),您也必须返回某些内容或抛出异常,否则您的代码将具有未定义的行为。
推荐阅读
- javascript - React 中的 Konva 无限网格
- ruby-on-rails - 更新 Amazon RDS SSL/TLS 证书 - Elastic Beanstalk
- kubernetes - 在 Kubernetes 中命名服务会阻止它启动
- c# - 在 Net Core 3.0 中同时使用 GRPC 通道
- javascript - 如何验证/阻止缩短字符串中的 URL
- python - Travis 收集...命令“pytest”以 1 退出
- excel - 范围内的 PowerQuery 范围
- linux - Linux 中给定命令的 CPU 利用率
- python - 创建知道每个列表长度的子列表
- c - 如何裁剪 SOIL 加载的图像