C++ Syntax: new

Description

The new operator creates an object in the heap memory and returns a pointer to it. For example:-
TH2F *H_TDC = new TH2F("H_TDC","TDCA vs TDCB",100,0.,600.,100,0.,600.);
creates a TH2F histogram object. new first allocate space for the object on the heap and then calls the object's initialisation routine (i.e. its constructor). As the above example shows, arguments can be supplied to the constructor by supplying them after the class name. An array of objects can be created using new:-
Int_t *my_ints = new Int_t[10];
although in this case, only the default constructor, i.e. the constructor to be called when no arguments, can be used.

In order to avoid memory leaks the delete operator must be called once the object is no longer needed.

See operator precedence

Other Uses for New

None.

Usage Notes

None.
Go Back to the The C++ Crib Top Page


If you have any comments about this page please send them to Nick West