-3

This is a basic histogram being created but my question in the line TH1F *hist=new TH1F("hist", "Histogram", 100, 0, 100); I know pointers help store an address to a object and constructors are helpful in inputting values to objects in a class but what is going in this line ? Is a pointer created and defined as a constructor ? and what is the use of the "new" ?

// Creating a histogram                                                                                                                                                                                            
void tut1()
// Void functions do not return values, simply prints a message so I assume our message here is the histogram, histograms display values but they are not themselves not values                                    
{
 TH1F *hist=new TH1F("hist", "Histogram", 100, 0, 100);
 // This is just a constructor                                                                                                                                                                                    
 // TH1F is a inherited class from the base class TH1                                                                                                                                                             
 //(the name of the histogram, the title of the histograms, number of bins, start of x axis, and ending paramater of x axis)                                                                                      
 // Here we are accessing TH1F  the capital F is for floats and we use this to use 1D histograms                                                                                                                  

 // To Fill the histogram we use                                                                                                                                                                                  
  hist->Fill(10);
  hist->Fill(40);

 // Add titles for the axis's                                                                                                                                                                                     

  hist->GetXaxis()-SetTitle("X Axis");
  hist->GetYaxis()-SetTitle("Y Axis");



  TCanvas *c1 = new TCanvas();
 hist->Draw();
   // Tcanvas is used to draw our plot it is the window that is used to display our image                                                                                                                         
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Abaraj48
  • 1
  • 4
  • 2
    I strongly recommend [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if your beginner book doesn't clearly explain what `new` does. – Drew Dormann Jun 24 '21 at 21:02
  • and you better get used to comments being liars. "// This is just a constructor" is strictly speaking non-sense. Its a call to a constructor not a constructor – 463035818_is_not_an_ai Jun 24 '21 at 21:05
  • _"constructors are helpful in inputting values to objects"_. Constructors **create** objects. Another word for "create" is "construct". – Drew Dormann Jun 24 '21 at 21:08
  • i think the root framework has some sort of garbage collection or lifetime mangagment going on in the bachground, not sure if the dupe is appropriate, on the other hand OP has to start somewhere – 463035818_is_not_an_ai Jun 24 '21 at 21:19
  • @DrewDormann okay so constructors are objects and we can use these objects to access variables or functions defined in a class for whatever's we are trying to do, so here the pointer points to a constructor that access's the histogram class, why would we need the pointer in this case ? – Abaraj48 Jun 24 '21 at 21:26
  • 1
    Constructors are not objects. They turn memory into an instance of a class (often called an "object", but watch out [as object has a much broader meaning in C++](https://en.cppreference.com/w/cpp/language/object)). After the constructor has finished, the pointer points to an instance of the class. Often you don't need a pointer at all. In C++ it is usually preferable to use [Automatic allocation](https://stackoverflow.com/questions/1534999) (`TH1F hist("hist", "Histogram", 100, 0, 100);`) where possible. This removes the need to manually manage the allocation provided by `new` – user4581301 Jun 24 '21 at 21:59
  • 1
    frankly, you get basic terminology wrong and really should spend some time with some introductory book, because C++ isnt a language you can learn by looking at random code examples. See here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Jun 24 '21 at 22:29
  • @463035818_is_not_a_number Thank you for the critique it helps in my journey of learning c++ – Abaraj48 Jun 26 '21 at 03:31
  • @user4581301 so TH1F *hist=new TH1F("hist", "Histogram", 100, 0, 100); we have a pointer hist that points to an object of class TH1F (so like creates address to the variables in the constructor ?) and that object is a constructor that is an instance of the class TH1F and this constructor is an object of the class that is a piece of memory, so like for instance int a; int would be like the class and 'a' is the constructor, and this constructor can access and initialize members of the class and the new is allocating enough memory for that object and calls for our object to initialize – Abaraj48 Jun 26 '21 at 03:52
  • @user4581301 members of the class ? so does the new and constructor both initialize the members if so is that why we would not need the new in such a simple code ? – Abaraj48 Jun 26 '21 at 03:53

1 Answers1

0

The variable hist is a pointer to type TH1F. It's assigned a value to a new instance of TH1F created by the constructor to it.

One thing I noticed was missing in your program is the corresponding delete statement.

Ben Y
  • 913
  • 6
  • 18
  • when you say new instance you mean like a new object right ? Also the constructor here is a parameter constructor that is accessing the 1D histogram class's X axis, number of bins and how to name the plot ? – Abaraj48 Jun 24 '21 at 21:14
  • Typically when a constructor takes arguments, it sets the object's members. If the plot's name is one of the members of the object, you would either directly set the member (public member) or make use of a method to change it. Not enough information is given here regarding the name of the plot, incidentally. – Ben Y Jun 24 '21 at 21:19
  • Oh okay I think I get it, why would we have to use a pointer with a constructor here ? – Abaraj48 Jun 24 '21 at 21:29
  • An automatic variable (it gets deleted when you leave the context) that serves the same purpose would be instantiated with ` TH1F hist("hist", "Histogram", 100, 0, 100);` For a simple example like this it doesn't seem to make sense, but for other kinds of uses, you may need to dynamically instantiate objects. – Ben Y Jun 24 '21 at 21:38
  • @BenY the histogram has to created dynamically because it is suppoed to live beyond the functions scope. The root-framework has some form of lifetime management behind the scences. `hist->Draw` implicitly attaches the histogram to the `Canvas` and when the user closes the canvas the histogram is removed too. Though, I think we can agree that OP should try to understand "normal" C++ lifetime, creation of objects etc, before they can understand exotics – 463035818_is_not_an_ai Jun 26 '21 at 09:01