Re: return type vs passing a reference
On Nov 17, 10:12 am, Triple-DES <DenPlettf...@gmail.com> wrote:
On 17 Nov, 07:20, Ian Collins <ian-n...@hotmail.com> wrote:
mail....@gmail.com wrote:
If you think the returned object from function may
generate any kind of exception during its construction or
assignment and any kind of memory leak or resource
allocation/deallocation misbehave then it would be better
to use reference technique otherwise you can use return
technique.
Why? The exception is still going to have to be caught.
I think his point is that if the function is a non-const
member function, and the "return t;" may throw (because T's
copy ctor may throw), the object will not be in the same state
as before the operation, making it impossible to give the
strong exception safety guarantee.
As a general rule, if the strong exception safety guarantee is
required, mutators shouldn't return anything but return codes.
The strong exception safety guarantee isn't needed that often,
however (or rather, when it is needed, it is generally needed at
a higher level of granularity, for tranactions involving several
objects).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=C3=A9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=C3=A9mard, 78210 St.-Cyr-l'=C3=89cole, France, +33 (0)1 30 23 00 =
34