每个新对象后进行 NULL 检查是否有任何用途?

问题描述

假设这里 new 会引发异常。

是否会检查 new 对象下面的 NULL 条件,因为它会返回异常,所以在每次 new 之后都要检查这个 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();
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)