question about 'new'

  • Thread starter Thread starter Bruno van Dooren
  • Start date Start date
B

Bruno van Dooren

Hi all,

MyClass Instance = NULL;
Instance = new MyClass();

and 'new' throws an exception.
is the value of Instance still NULL, or is it undefined?

I looked in MSDN, but could not really find out.

kind regards,
Bruno.
 
Bruno said:
MyClass Instance = NULL;
Instance = new MyClass();

and 'new' throws an exception.
is the value of Instance still NULL, or is it undefined?

I'd think that the assignment wouldn't execute, because the exception
is thrown while it's evaluating the righthand side (before the value
to assign has been determined). Someone else should be able to give
you chapter and verse.
 
MyClass* Instance = 0;
I'd think that the assignment wouldn't execute, because the exception
is thrown while it's evaluating the righthand side (before the value
to assign has been determined). Someone else should be able to give
you chapter and verse.

That's absolutely correct. Normal C++ expression rules cover this: the
lefthand side of an assignment cannot be assigned to until the righthand
side has been evaluated, and a new expression isn't fully evaluated until
allocation has succeeded or a constructor has run to completion. Any
exception thrown from the allocation function or the constructor prevents
the expression from being fully evaluated, so the left hand side of the
assignment is guaranteed to have not been assigned.

-cd
 
Back
Top