Re: [ROOT] private TCollection copy ctor?

From: George Heintzelman (georgeh@sparrowhawk.chm.bnl.gov)
Date: Fri Sep 01 2000 - 17:39:57 MEST


> Hi George, Hi Victor,
> 
> I think that you are both right.
> George's request to copy only the pointers part of a collection without
> copying the objects is perfectly legitimate and should be supported.
> As Victor says, doing this via the copy constructor is extremely dangerous and
> with many side-effects.
>
> What about having a new function in TCollection and derived classes,
> say TCollection::ClonePointers to do the job in the same way that we already
> have the Clone function for any type of TObject?

The problem is that without a public copy constructor you can never 
return a collection by value from any function. While it is true that 
this does open you to some potential mistakes by users of the class 
(particularly, as Victor pointed out, with default copy constructors 
generated for people who have TCollection members and want to own the 
contents of that collection), it is also a significant loss of 
functionality. Usually, it is true, you will want to pass collections 
and so on by reference, but preventing a function return by value 
prevents one from writing functions that produce any sort of filtered 
collection (or forces the user of such functions to explicitly delete a 
returned pointer).

I also note that as Fons helpfully pointed out, the TObjArray in 
particular, (unlike the other TCollections) DOES have a public copy 
ctor, which is what I'm using to solve my problem right now. Certainly, 
whatever decision is reached should be applied consistently to all ROOT 
containers.

The proposed extension might be a nice convenience, but would not have 
solved my particular problem for non-TObjArray containers. As it 
happens, performance is not too much of an issue and TObjArray is 
satisfactory for what I need (TOrdCollection would have been 
preferable, but TObjArray is workable).

To assist people who *do* want to own the objects contained within an 
owned TList, it would be nice to supply some quasi-copy constructor 
which Clone()'s each of the contained objects... eg, TObjArray(const 
TObjArray &, TCollection::kCloneContents) or something. Clone() doesn't 
do this job very well for objects with TCollection-derived members (not 
TCollection* members, for which Clone() is fine). Also, having such an 
explicit constructor might help to point out some of the dangers of the 
default copy constructor.

George Heintzelman
gah@bnl.gov



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