Copy constructor of unkown TObject delivered class

Hi,
is there some way to call copy constructor of any TObject based class? I know that I can use TClass for calling default constructor but I have know idea is this is possible to call copy constructor in such way.
Unfortunately I cannot use Clone method.


ROOT Version (e.g. 6.08):
Platform, compiler (e.g. Min 17, gcc4.8):


Hi,

I think it’s not possible to use TClass to invoke a particular copy constructor. Note that TClass lets you access the representation of all classes known to the type system, not only the ones inheriting from TObject.

Can I help you out nevertheless? What are you trying to achieve?

Cheers,
D

Hi,
I have class that I need to copy however my copy constructor do init-like stuff so I suppose using Clone will not help (because this make copy of my object).
Actually I can just do something like this:
BaseCopy *copy = (BaseCopy)myObj->Clone();
copy->Overwrite(myObj);
It’s just less “nice” approach.

If you inherit from TObject (or TNamed), you can implement you own Clone and/or Copy methods (they’re “virtual”).

To copy an TObject (or a class derived from), you can call the ‘default’ (usually no need to overload) Clone:

MyClass *myobject = ....;
MyClass *mycopy = dynamic_cast<MyClass *>(myobject->Clone());

This will use the I/O sub-system to copy the object (one important consequence is that transient data member are not copied but left with the value given by the default constructor).

Cheers,
Philippe.

If you need to ‘update’ an existing object rather than using Clone (which allocates) it, then you can try (to write a small wrapper function that does)

     TBufferFile buffer(TBuffer::kWrite,bufsize);
     buffer.MapObject(obj);  //register obj in map to handle self reference
     input->Streamer(buffer);
     // here some additional compliciation needed if you use TRef  in conjunction with those objects.

      // read new object from buffer
      buffer.SetReadMode();
      buffer.ResetMap();
      buffer.SetBufferOffset(0);

      buffer.MapObject(newobj);  //register obj in map to handle self reference
      newobj->Streamer(buffer);
      newobj->ResetBit(kIsReferenced);
      newobj->ResetBit(kCanDelete);

Cheers,
Philippe.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.