samedi 13 février 2016

Best way to make a child object from a list self convert into other child of same parent

I have a list of pointers to objects from Parent

vector<Parent*> list;

then I have 2 child Classes of Parent: Child1 and Child2 that are added to the list.

The code runs through all entities of the list and calls Execute();

for(int i = 0; i < list.size(); i++)
    list[i]->Execute();

Execute() is a virtual function of Parent and is implemented differently in each child class. I want to make a condition inside the function of Child1::Execute() in which it is replaced by an entity of Child2 at the same position in the list.

of course I cant do this:

void Child1::Execute() {
    ...
    if(condition)
        this = new Child2();
}    

But I cant figure out a good solution that does this. In the end there will be a few hundred Other Child classes from Parent that will do the same but in other conditions, so I wanted a good expandable solution.

Thanks

Aucun commentaire:

Enregistrer un commentaire