c++recursiontcollectiontcollectionitem

Recursive read of TCollection


I'm very bad with recursion, never used it before. I know the theory of it .. not that that helps :)) For my problem i have a structure of TCollection that contains TCollection and TCollectionItem etc .. I have to write a recursion function that will read all my TCollectionItems. Here is graphical view:

TCollection->TCollectionItem(s)->TCollection->TCollectionItem(s)

TCollection can have 1 or even 2,3 TCollection's under him OR none.

Here are few more examples:

TCollection->TCollectionItem

TCollection->TCollectionItem->TCollection->TCollectionItem->TCollection->TCollectionItem

etc ..

Please tell me if i described the problem badly, i probably did .. please ask if something is unclear :)

Thanks for the support!


Solution

  • You haven't indicated the prototypes of the TCollection methods so as to enumerate and to read your TCollectionItems, and other needed details.

    However, this is definitely solved by: The Composite Design Pattern.

    The aim of this pattern is to traverse a recursive form, and to forward a call on a composite onto its composants and so on, until that reaches the leaves ( TCollectionItems with an empty TCollection in your case)