Skip to content

Commit d05d6de

Browse files
committed
[Tree] Delete virtual copy and copy assignment methods in TTree
The `TTree` class already deletes the copy constructor and the copy assignment, but users might still be tempted to use the virtual copy constructor or copy assignment provided by `TObject::Clone()` and `TObject::Copy()`. But these implementations also make no sense to the TTree. To avoid user confusion, this commit suggests to override these methods in TTree such that they thow an exception and tell you what to use instead. Prevents reports this one in the future: * https://its.cern.ch/jira/browse/ROOT-9111
1 parent 0ba8001 commit d05d6de

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

tree/tree/inc/TTree.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ class TTree : public TNamed, public TAttLine, public TAttFill, public TAttMarker
340340
TTree(const TTree& tt) = delete;
341341
TTree& operator=(const TTree& tt) = delete;
342342

343+
TObject *Clone(const char *newname="") const override;
344+
void Copy(TObject &object) const override;
345+
343346
virtual Int_t AddBranchToCache(const char *bname, bool subbranches = false);
344347
virtual Int_t AddBranchToCache(TBranch *branch, bool subbranches = false);
345348
virtual Int_t DropBranchFromCache(const char *bname, bool subbranches = false);

tree/tree/src/TTree.cxx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ End_Macro
466466
#include <climits>
467467
#include <algorithm>
468468
#include <set>
469+
#include <stdexcept>
469470

470471
#ifdef R__USE_IMT
471472
#include "ROOT/TThreadExecutor.hxx"
@@ -1060,6 +1061,17 @@ TTree::~TTree()
10601061
}
10611062
}
10621063

1064+
TObject *TTree::Clone(const char *) const
1065+
{
1066+
throw std::runtime_error("TObject::Clone() is not supported by TTree! Use TTree::CloneTree().");
1067+
}
1068+
1069+
void TTree::Copy(TObject &) const
1070+
{
1071+
throw std::runtime_error(
1072+
"TObject::Copy() is not supported by TTree! Use TTree::CopyTree() or TTree::CopyEntries().");
1073+
}
1074+
10631075
////////////////////////////////////////////////////////////////////////////////
10641076
/// Returns the transient buffer currently used by this TTree for reading/writing baskets.
10651077

@@ -3205,7 +3217,7 @@ TTree* TTree::CloneTree(Long64_t nentries /* = -1 */, Option_t* option /* = "" *
32053217

32063218
// Note: For a chain, the returned clone will be
32073219
// a clone of the chain's first tree.
3208-
TTree* newtree = (TTree*) thistree->Clone();
3220+
TTree* newtree = (TTree*) thistree->TNamed::Clone();
32093221
if (!newtree) {
32103222
return nullptr;
32113223
}

0 commit comments

Comments
 (0)