Quote:Original post by Subotron
ok, guess I have to put in a different question:
suppose at some point in my constructor something fails (the initialization function returns false), can I then call the destructor from within the constructor?
Like this:class::class(){ if (!initialize) Call_destructor_here();}
Will the compiler allow you to do this? Yes.
Should you do this? God no.
Since a constructor cannot return a value to indicate failure, you do not allow the classes clients to know there was a failure, so they will use an invalid object as if it was valid. Better to cleanup and throw an exception.
It is rare that you need to call a destructor explicitly. It is even rarer for such code not to have an alternate that is simpler. (eg, moving common cleanup to another function, and adding a member variable to indicate whether the class is initialised properly...
Edit: also remember that if you call a destructor explicitly, the destuctor will be called again when delete is used, or when a stack object goes out of scope. Your destructor must be capable of being called twice.