# Why using pointer of classes

**URL:** <https://root-forum.cern.ch/t/why-using-pointer-of-classes/54657>\
**Category:** Newbie\
**Tags:** root\
**Created:** [April 27, 2023, 3:16am UTC](https://root-forum.cern.ch/t/why-using-pointer-of-classes/54657 "2023-04-27T03:16:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [April 27, 2023, 3:16am UTC](https://root-forum.cern.ch/t/why-using-pointer-of-classes/54657/1 "2023-04-27T03:16:16Z")

</div>

Dear Experts

I’m new to c++, I saw many people using pointer to define ROOT class member like:

```auto
TTree* t = //some Get function
TH2D* h = new TH2D();
TCanvas* c = new Canvas();

```

instead of

```auto
TTree t = //some Get function
TH2D h;

```

What’s the difference and what advantage will I have if I use pointer to define them?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 27, 2023, 7:28am UTC](https://root-forum.cern.ch/t/why-using-pointer-of-classes/54657/2 "2023-04-27T07:28:29Z")

</div>

### file canvas1.C

```auto
void canvas1() {
   TCanvas c;
   TText t (.5,.5,"Hello");
   t.Draw();
}

```

### file canvas2.C

```auto
void canvas2() {
   auto c = new TCanvas();
   auto t = new TText(.5,.5,"Hello");
   t->Draw();
}

```

Then try:

```auto
root[0] .x canvas1.C

```

and

```auto
root[0] .x canvas2.C

```

and you will see the difference.

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [April 27, 2023, 8:06am UTC](https://root-forum.cern.ch/t/why-using-pointer-of-classes/54657/3 "2023-04-27T08:06:13Z")

</div>

The first one doesn’t give anything on the screen, where the 2nd one gives canvas and text on the canvas…Why?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 27, 2023, 8:10am UTC](https://root-forum.cern.ch/t/why-using-pointer-of-classes/54657/4 "2023-04-27T08:10:44Z")

</div>

Because with the 1st one, the object is local: created in the macro and deleted when the macro exits. In the 2nd one, the pointer stays alive.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [April 27, 2023, 8:11am UTC](https://root-forum.cern.ch/t/why-using-pointer-of-classes/54657/5 "2023-04-27T08:11:39Z")

</div>

The same question was asked 10 years ago: [Why do so many use pointers to ROOT objects?](https://root-forum.cern.ch/t/why-do-so-many-use-pointers-to-root-objects/15691)

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [April 28, 2023, 2:26am UTC](https://root-forum.cern.ch/t/why-using-pointer-of-classes/54657/6 "2023-04-28T02:26:34Z")

</div>

Thank you for the explanation, understood that pointer will stay alive after macro exits. What if I write a c++ program and compile with root? Will it be any difference between pointer and object? Like:

```auto
int main(){
   TTree* t =
}

```

and

```auto
int main(){
   TTree t;
}

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 28, 2023, 4:48am UTC](https://root-forum.cern.ch/t/why-using-pointer-of-classes/54657/7 "2023-04-28T04:48:11Z")

</div>

Then that’s a [C++ question](https://stackoverflow.com/questions/5046669/main-differences-between-objects-and-object-pointers). Normal objects vanish automatically when they are out of scope (basically when exiting `{ } `) and pointers must be deleted otherwise you get a memory leak.
