Get pointer to a container object

Hello,

My problem is the following: I have an Event class which contains several tracks which are written to a Track class. Some of the methods of Track are utilizing the information contained in the Event object (like incident particle energy, direction of the projectile etc) and I would like to find a convenient mechanism to get this data.

So, I have 2 classes of the following structure:

class Event : public TObject {
  Track *track;
  ...
}

class Track : public TObject {
...
}

Is it possible to get a pointer to a container object (class Event) from an object of class Track? Of course, I could set and save the pointer to Event in the constructor/method of Track, but I wonder whether there is another way to do it. I mean, is there a method like Track::GetContainerObject()?

[quote] I mean, is there a method like Track::GetContainerObject()[/quote]No unless the implementer of Track provided it. (I.e. you will need to instrument Track to do what you need).

Cheers,
Philippe

So, the only way is to keep the pointer on Event in each Track object, right? The problem is that there are 10000+ tracks in each event, so these pointers will take much space.

As an alternative, one could pass the pointer on Event as an argument of a Track method which needs it… but I do not think it is convenient…

[quote=“pcanal”]No unless the implementer of Track provided it. (I.e. you will need to instrument Track to do what you need).
[/quote]
How do I implement this in Track?

Hi,

Storing the Event pointer in the Track object or passing the Event pointer to the function that needs are the only recommended solution. Your other alternative (which is strongly discouraged because of its many pitfalls and problems) is to store the Event pointer in a global variable.

Cheers,
Philippe.