Re: return type vs passing a reference
On Nov 17, 6:49 am, mail....@gmail.com wrote:
On Nov 16, 1:37 am, Carl Forsman <fatwallet...@yahoo.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.
This is simply wrong. If the object is going to leak, it's
going to leak in both cases. (Well designed and implemented
objects don't leak, even if there is an exception.)
As you can see in STL stack class pop() method doesn't return
anything. You'll have to use top() method to retrieve top most
element of stack.
That's a totally different issue. The std::stack<>::pop()
doesn't return anything; the question here is how to return
something. And the justification in the case of pop has nothing
to do with possible resource leaks; it's about maintaining a
strong exception guarantee, in which every action either returns
normally or makes NO change in state. This strong guarantee
isn't really necessary that often, and returning the value via a
reference wouldn't make it any easier to meet.
--
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