This will "work" (but see below):
activeInstance.reset(this);
The problem is, what does it mean? When activeInstance
goes out of scope, this
will be delete
d. That may not be what you want. You should also read about enable_shared_from_this
, which would allow you to say:
activeInstance = shared_from_this();
Another option is to use a "null deleter", that is, specify a deleter function which does nothing:
void NoDelete(void*) {}
activeInstance.reset(this, NoDelete);
In many cases this will be a safe and correct solution, assuming that this
will be deleted by some other method elsewhere, and not before the last dereference of activeInstance
.