question about 'new'

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.
 
C

Craig Powers

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.
 
C

Carl Daniel [VC++ MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top