Re: copy

From: Axel Naumann <Axel.Naumann_at_cern.ch>
Date: Thu, 22 Jun 2006 10:48:16 +0200


Hi,

OKUMURA, Akira wrote:
> Hello ROOTers,
>
> I have several questions about copy constructors and assignment operators.
>
> 1) Automatic copy constructor and assignment operator
> ROOT classes have copy constructor and assignment operator which are
> documented in html.
> However some of them are not implemented in their sources nor headers.
> For example,
> http://root.cern.ch/root/htmldoc//TSpectrum.html#TSpectrum:operator=
> Does this mean that copy constructors and assignment operators will be
> created by compilers automatically ?
> If so, it is very confusing if the automatic ones are listed in the html
> document.

That's a good point - the documentation part. These copy constructors / op= are declared but not implemented to _prevent_ the compiler from generating a default one, because the author of the class has decided that the execution of its proper, hand-written version is too costly, and these objects should not be _accidentally_ copied, only with an explicit call to Clone(), and the automatically generated version is insufficient. It's also about shallow copies (which are the ones provided by the compiler) - they usually have side effects that the author of the class foresaw, and wanted to prevent. Uh, I see you're mentioning this yourself.

So it's perfectly legal to declare them but not implement them (so the user accidentally using them gets a linker error). But this should be documented, and currently a) THtml doesn't try to document which methods don't have an implementation, and b) we don't have a doc syntax that fits to methods without method blocks. The latter will change in about two weeks.

> 2) Deep copy and shallow copy
> As to TSpectrum, its assignment operator seems to copy objects as
> 'shallow copy' in spite of its pointer members.
> Meanwhile e.g. TH1D has own (not automatic) assignment operator which
> uses ::Copy() inside it.
> What is the difference between these two coding styles ?

>From what you describe this is inconsistent, and it should be fixed in
TSpectrum - but I didn't look at the code. I'm sure the authors and / or Rene will comment.

> 3) assignment operator, copy constructor, Copy() and Clone()
> How to determine appropriate copier from the above without reading the
> ROOT source ?

copy c'tor: shallow copy, use if there's no allocation which would be shared by two objects. ROOT tries to forbid the usage of this copy c'tor if a shallow copy is not enough, by declaring but not implementing it.

assignment op: same thing when it comes to shallowness. Theoretically a bit less efficient (due to the return value, a temporary object, which should get optimizerd away by any reasonable modern compiler).

Copy(): like Clone, except where stated. A little less deep copy, usually ignoring members that are not always needed.

Clone(): a full, complete, deep copy of an object.

Cheers, Axel. Received on Thu Jun 22 2006 - 10:48:27 MEST

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:31:59 MET