Why pointers?

Dear rooters,
I am curious, why we use always pointers?
All the examples in the manual have pointers.
Is there any specific reason for doing this(e.g. memory usage) or not?

[quote=“magistros”]Dear rooters,
I am curious, why we use always pointers?
All the examples in the manual have pointers.
Is there any specific reason for doing this(e.g. memory usage) or not?[/quote]

Well, it depends: sometimes, you want your objects to live after your macro execution finished. For example, you have a named macro + create canvas/histograms in your macro and want to show your plots - it’s obvious, that lifetime of both canvas and histograms must be longer than macro-execution time. Another reason - if you are using ROOT’s object’s ownership, for example, you want a TFile you created to take an ownership of all trees/histograms you created after this - in this case you obviously can not use stack objects (if you haven’t done this yet, I’d recommend you to read the chapter about object ownership in ROOT’s manuals - it will help you to avoid some annoying and subtle problems).

So, I’d say, the general reason is a lifetime management - object you created must live longer.
If not - usually there is not reason to create an object using new and pointers - with stack objects you do not have to care about ‘delete’ and things like this.

Also, sometimes it’s just an abuse - instead of creating a stack object with short lifetime, people create this object on heap without any real reasons except their “habits” - but I do not think we have such cases in our tutorials.

I see…
Thank you very much for your reply :smiley: