samedi 26 mai 2018

C++ set objects within an array to NULL

I have a class Modal

class Modal {
private:
   A* obj1;
   B* obj2;
}

where A is B's parent class

class A {
};

class B : public A {
};

in the destructor of Modal I want to set obj1 and obj2 to NULL

I created a convenience method:

void remove(A*& obj) {
    if (obj == NULL) { return; }
    obj = NULL;
}

and in the destructor I do something like this:

A* objects[] = { obj1, obj2 };
for(auto obj: objects) { remove(obj); }

But it does not set the obj1 and obj2 values to NULL If I call directly:

remove(obj1);

it works as it should. So what am I doing wrong with the array?

Also: The reason why I put obj1 and obj2 into an array is because I will have more objects and I want to keep the code for the removal short.

Aucun commentaire:

Enregistrer un commentaire