In this answer, Ryan directly calls the virtual destructor. I've tested the code in VS2010, and it correctly calls all destructors (tested with logging statements). Is it actually valid to do so? What are the problems, flaws or even the good points of such an approach?
I can only think of it as a way to really force a reset of the actual type, even if they don't override a virtual reset
function, since they atleast have to clean up in their destructors.
Also, eactly what kind of side-effects does a call to the destructor bring? Is it undefined behaviour to use the object after such a destructor call? What if one immediatly reinitializes it with a new (this) MyClass();
call?
new (this) MyClass();
is basically the only thing that you can do with the object after calling the destructor (i.e. reconstructing it). – Bushmanreset()
, because if you do then someone's going to (invalidly) attempt to destroy it later. Possibly you couldterminate()
. – Ormondconst
objects involved, and I suspect that another potential source of bugs is that you can't in general destroy an object of a derived type (via its virtual destructor) and then re-create it as a base type. – Ormondconst
variable? – Giulioconst
variable, a reference, or to change the real type of an object. BTW, don't forget that it is also forbidden to just observe a legal change to the value of aconst
variable. – Giulio