On Mar 23, 10:48 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
I was revisiting some code and I came across this:
Class Client
{
// ...
void ResetClass()
{
COptions SaveOptions = Options;
this->~CClient();
new (this) CClient();
Options = SaveOptions;
}
// ...
};
That use of calling delete and placement new on this seems dubious and I
wonder if it's effects are well defined or undefined.
In this example it certainly IS dubious, since your class is Client
and you are newing a CClient.
But assuming your class name is a typo, then the issue is "what
happens if new(this)CClient throws an exception?" Then the object was
destroyed, but failed to reset. You are left with a dead object.
That is bad news, and why this technique --although well defined for
the non-throwing case-- is dubious.
It might have a throw() (non-throwing) constructor. No? The constructor is
not shown if it throws or not.