question about new and delete

Feb 29, 2008 at 11:38pm
I am currently learning about new and delete objects on the heap. many of the examples are classes calling new in the constructor. i was under the impression you shouldnt call new in a constructor because new can fail. am i being paranoid or is it ok.
Mar 1, 2008 at 3:34pm
You are perfectly safe.

If something goes wrong in the constructor you should throw an exception. By default, 'new' throws an exception on failure. Hence, things are fine --your constructor will throw an exception if new fails.

The only thing to be aware of is that you should NEVER (and this really is a NEVER) throw an exception in a destructor. Read more at the C++ FAQ Lite (sections 17.2 and 17.3).

Hope this helps.
Topic archived. No new replies allowed.