mardi 9 juin 2015

State design pattern - delete this in C++ [duplicate]

This question already has an answer here:

I am using the example below to implement a state design pattern.

http://ift.tt/1B2XdvI

I am concerned about delete a this pointer inside a member class:

void on(Machine *m)
{
    m->setCurrent(new ON());
    delete this;
}

Is it safe to do that? What could happens if I call other member function after delete?

Is there a better approach?

Best regards,

Aucun commentaire:

Enregistrer un commentaire