Er'body says that do not call delete this from the member function, but to notify the owner that the object is ready to be deleted.
I have a service that creates workers, but does not care about them after they are created.
From the class of service:
Worker* w; [...] while (1) { [...] w = new Worker(); [...] }
Then from within the working class:
void Worker::doWork() { [...] [...] delete this; }
The entire service class makes new workers, so there are no references to the worker outside the working class, except for the pointer w , which is discarded every time a new worker is created.
In this case, I think it makes sense to call delete this inside the worker when he has nothing more to do (while delete this; is the last statement in doWork() and there are no successful member calls after doWork() .
If you saved each instance of the created worker, this will not be so.
source share