lundi 26 septembre 2016

Is it bad to do something like this?

Is it bad to do something like this? (not checking for nullptr inside the draw() function before doing operations with the object pointer)

class SomeClass
{
public:
    SomeClass(Object& someValidObject)
    {
       object = &someValidObject;
    }

    void draw()
    {
        object->draw();
    }

    Object* object = nullptr;
}

Or should I always check for nullptr before calling operations in the object, even though the constructor is for sure going to make the pointer point to something?

void draw()
{
   if(object != nullptr) 
      object->draw?
}

Aucun commentaire:

Enregistrer un commentaire