# Adding MakeIterator() method to TObject class?

**URL:** <https://root-forum.cern.ch/t/adding-makeiterator-method-to-tobject-class/55023>\
**Category:** Newbie\
**Created:** [May 25, 2023, 7:08am UTC](https://root-forum.cern.ch/t/adding-makeiterator-method-to-tobject-class/55023 "2023-05-25T07:08:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tspark](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@tspark](https://root-forum.cern.ch/u/tspark)\
**Post date:** [May 25, 2023, 7:08am UTC](https://root-forum.cern.ch/t/adding-makeiterator-method-to-tobject-class/55023/1 "2023-05-25T07:08:40Z")

</div>

Hi,

I am wondering if there is a possibility to add the following method to TObject class:

```auto
virtual TIterator * TObject::MakeIterator	(Bool_t dir = kIterForward	)	const { MayNotUse(""); return 0; }

```

The thing is that, when I develop a library, defining

```auto
class TMyObject : public TObject {...}
class TMyCollection : public TMyObject { ...}

```

then I cannot call

```auto
TIter next(TMyCollection*);

```

because TMyCollection does not inherit from TCollection.  
The way of defining

```auto
class TMyCollection : public TCollection { ...}

```

is not good because it makes difficult to treat TMyObject and TMyCollection consistently.

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [May 25, 2023, 7:36am UTC](https://root-forum.cern.ch/t/adding-makeiterator-method-to-tobject-class/55023/2 "2023-05-25T07:36:16Z")

</div>

Hi @tspark ,

> [@tspark](#):
>
> is not good because it makes difficult to treat TMyObject and TMyCollection consistently.

I wonder why, since TCollection inherits from TObject anyway.

In any case, I do not think modifying/increasing features of TObject is something we want to do at this point, unless in case it fixes outstanding bugs.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![tspark](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@tspark](https://root-forum.cern.ch/u/tspark)\
**Post date:** [May 25, 2023, 7:50am UTC](https://root-forum.cern.ch/t/adding-makeiterator-method-to-tobject-class/55023/3 "2023-05-25T07:50:49Z")

</div>

Dear Vincenzo,  
Thanks for your reply.  
What I meant is that, while TMyObject contains features I added on top of TObject, but TCollection does not contain those added features. There can be a few possible workarounds, but expanding TObject will resolve the issue most neatly, I guess.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [May 25, 2023, 11:58pm UTC](https://root-forum.cern.ch/t/adding-makeiterator-method-to-tobject-class/55023/4 "2023-05-25T23:58:35Z")

</div>

> I am wondering if there is a possibility to add the following method to TObject class:

Just adding this method would not solve the problem, would it? We would also need to change `TIter::TIter` to accept a `TObject` rather than a `TCollection` and then this would make things less type-safe (now any non-collection would be able to compile fine being based to a `TIter` but would have to fail at run-time).

Even for new ROOT code, we have migrated away from using `TIter` anyway and have instrument `TCollection` (and its derived class) so that the normal `range for loop` work well with them:

```auto
for(auto obj : *my_tcollection) ...
...
for (auto bcl : TRangeDynCast<TClass>(*cl->GetListOfBases())) {

```

and those could be applied to your case without inheriting from `TCollection`

---

<div class="post-metadata">

**Author:** ![tspark](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@tspark](https://root-forum.cern.ch/u/tspark)\
**Post date:** [May 26, 2023, 1:39am UTC](https://root-forum.cern.ch/t/adding-makeiterator-method-to-tobject-class/55023/5 "2023-05-26T01:39:14Z")

</div>

Dear pcanal,

For the 1st paragraph: Yes, you are right, and that’s why I put MayNotUse(), but it will still be less type-safe.

For the 2nd part: I am not familiar with auto, and never heard about TRangeDynCast. I am just finding that it works smoothly, thank you very much for your suggestion!

One more question. In the example given in the head file of TCollection, they check if the auto variable bcl is null inside of the for-loop.  
My question is, when can it be null? (Or, is this checking really needed?)  
In my naive guess, it will be always non-null …

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [May 26, 2023, 2:19am UTC](https://root-forum.cern.ch/t/adding-makeiterator-method-to-tobject-class/55023/6 "2023-05-26T02:19:42Z")

</div>

If you are asking about the example with the `TRangeDynCast`, the iterator does the equivalent of a `C++` `dynamic_cast` and thus the item can be a `nullptr` if the element is not “deriving from” and/or “of” the specified type.

---

<div class="post-metadata">

**Author:** ![tspark](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@tspark](https://root-forum.cern.ch/u/tspark)\
**Post date:** [May 26, 2023, 3:11am UTC](https://root-forum.cern.ch/t/adding-makeiterator-method-to-tobject-class/55023/7 "2023-05-26T03:11:41Z")

</div>

Many thanks for your help!
