samedi 17 avril 2021

Does NULL check after every new Object serve any purpose? [duplicate]

Suppose here new will raise exception.

Will the NULL condition below new of object will ever be checked as on exception it will return , So is there point to have this NULL check after every new?

#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();
}

Aucun commentaire:

Enregistrer un commentaire