Re: Copy constructors

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Oct 15 1999 - 19:05:06 MEST


Hi Stefano,
Apologies for the long time to answer this mail.
I strongly recommend to use the Clone function to get a clone/copy
of any object. Clone is using the Streamer function of your class
to copy the complete object network and to rebuild it.
In your example, if you have an existing TGeometry *g1,
you can make a complete copy with
  THeometry *g2 = (TGeometry*)g1->Clone();

Rene Brun


Stefano Bettelli wrote:
> 
> Hi ROOTers,
> does anybody know how to copy an object like a
> TDirectory with lots of internal pointers without
> an appropriate copy constructor (or assignement
> operator)? For instance:
> 
> TGeometry *g, *new_g;                   // geometry pointers
> < ... load a directory in dir ... >
> g = (TGeometry *)(dir->Get("MYGEOM"));  // get a geometry
> new_g = new TGeometry(*g);              // try to build a copy
> < ... work on new_g ... >
> delete new_g;                           // get rid of the copy
> g = (TGeometry *)(dir->Get("MYGEOM"));  // try to build a fresh copy but ...
> new_g = new TGeometry(*g);              // ... this gives segmentation violation
> 
> This happens (I figure out) because the destructor of a
> TGeometry takes care to deallocate some objects which
> won't be referenceable any more by the original TGeometry.
> Is it a feature? (not to have copy constructors or assignement
> operators I mean).
> 
> ----------------------------------------------------------
> E-Mail: Stefano Bettelli <bettelli@tn.infn.it>
> WWW   : http://meitner.tn.infn.it



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:41 MET