On 27-Sep-1999, D. Tweed <[EMAIL PROTECTED]> wrote:
> One small comment is that in your functions condition1 & condition2 I
> think most C++ programmers would say that you want to write
> 
> int condition1 (const vector<long>& x)
>  
> since otherwise the compiler generally has to obey the normal function
> call semantics and create a copy of the vector when it passes it the
> function, rather than work directly with the existing list.

Yes, when writing C++, objects with expensive copy constructors
should always be passed by const reference, not by value.

> Personally I'd
> always write the above, not so much for performance reasons as the fact
> that if the objects in the vector have a shallow copy constructor
> (generated automatically & silently)  but a destructor that deallocates
> resources you've got an awful mess to debug when it crashes after leaving
> the function; consequently I do this even when it isn't strictly
> necessary. The few other C++ programmers I know do the same thing so
> it's probably reasonable to assume everyone does. 

That's not a reasonable assumption.  If you have a class which has a
shallow copy constructor but a destructor that deallocates resources,
then you're already in deep trouble.  Passing by const reference in
such a case will at best only stave off the inevitable disaster.

Your conclusion is correct, in this case, but the motivation should
be performance, not defending against buggy code with mismatched
destructors and copy-constructors.

-- 
Fergus Henderson <[EMAIL PROTECTED]>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger [EMAIL PROTECTED]        |     -- the last words of T. S. Garp.



Reply via email to