Re: Exception Handling constructor
On Jan 13, 1:22 pm, Sunil Varma <sunil....@gmail.com> wrote:
If a constructor of a class is in a try block, then it's
better creating an object dynamically than creating it
statically, for that class.
Just the opposite, I'd say. If you allocate using new, and
something throws later, you have to delete in the throw block.
Except that in many cases, there will be no pointer to the
object in the throw block. In general:
-- don't allocate dynamically unless you have to;
-- if the lifetime of the object is local (e.g. you have to
allocate dynamically because the object is polymorphic,
although it has the same lifetime as a normal local object),
use a smart pointer (boost::scoped_ptr or std::auto_ptr);
and
-- if the lifetime of the object is really dynamic, keep it in
a smart pointer until it has been "exported"---there is a
pointer to the object where ever the pointer to the object
is expected to be. (std::auto_ptr is very good for this.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34