c++ - 在每个新对象之后检查 NULL 是否有任何目的?
问题描述
假设这里 new 将引发异常。
是否会检查对象 new 下方的 NULL 条件,因为它会返回异常,那么在每个新对象之后进行此 NULL 检查是否有意义?
#include <iostream>
using namespace std;
class Obj
{
int x;
public:
Obj() : x(0)
{
}
~Obj() = default;
};
Obj *CreateObject()
{
Obj *o = new Obj; //suppose here new will raise exception
//Will This condition will ever be checked as on exception it will return , SO is there point to have this NULL check ??
if (!o)
{
cout << "Object creation failed";
return nullptr;
}
return o;
}
int main()
{
CreateObject();
}
解决方案
推荐阅读
- r - 如何从减去两个日期的结果中仅提取数字?
- python - 如何在 python 的这个函数中放置一个 df.iloc
- typescript - 如何告诉打字稿 map.get 将始终返回一个值?
- visual-studio-code - 如何在 VS Code 中使用 Vim 仿真完成嬉皮士?
- python - 将循环理解转换为 numpy 形式
- django - Django 扩展 shell_plus 与 jupyter 实验室
- amazon-web-services - 阻止对给定国家/地区的所有 aws 资源的访问
- python - 在我的 Django 中为博客文章创建一个赞按钮?
- reactjs - 如何在反应原生中使用反应导航添加类似顶部选项卡的whatsapp
- c - 增量编码器 - 轮询或中断?