Re: uh oh, copy semantics

From: Peter Malzacher (malzache@fnal.gov)
Date: Mon Feb 14 2000 - 23:57:20 MET


Hi Jeff,

> 
> if i have
> 
>         TLorentzVector *v1, *v2;
>         TLorentzVector v3, v4;
> 
>         // do something to give values to v1 and v3 ...
> 
>         v2 = v1;
>         v4 = v3;
>         *v2 = *v1;
> 
> are these assignments all doing the same thing?  I might suspect that
> the first one makes v2 point to the same object to which v1 is
> momentarily pointing.  

you are right
> However the second assignment would appear to
> depend on C++'s semantics for shallow vs. deep copies, as would the
> third form, and I would not know whether to expect the second and
> third forms to be equivalent.

The second and the third are equivalent, they use the operater=
of TLorentzVector. If you don't implement one C++ provides one with
shallow copy sematics. In TLorentzVector there is one implemented
with deep copy semantics.

> 
> I don't know how to interpret:
> 
>         TLorentzVector& operator=(const TLorentzVector& q)
> 

for the argument (const TLorentzVector& q) see Lipmann (3rd edition) p 341
last paragraph: reference arguments to avoid copying objects, const to 
                signal the compiler that you do not chage the argument.

At first you may think that void operator=(..) would be ok, because
v3 = v4 would be resolved as v3.operator=(v4), but in C++ such constructs
return a reference to the result to allow eg
if ( v3 == (v4 = *v1) ) ...
Therefore it seems resonable to mimic that behavior. 
see Lipman (3rd edition) p729-732 or 751-754

Peter



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:19 MET