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):
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?
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();
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).
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)
buffer.MapObject(obj); //register obj in map to handle self reference
// here some additional compliciation needed if you use TRef in conjunction with those objects.
// read new object from buffer
buffer.MapObject(newobj); //register obj in map to handle self reference
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.