Initializing Dynamic Memory

You can provide an initializer for a newly created fundamental-type variable, as in

double *ptr = new double( 3.14159 );

which initializes a newly created double to 3.14159 and assigns the resulting pointer to ptr. The same syntax can be used to specify a comma-separated list of arguments to the constructor of an object. For example,

Time *timePtr = new Time( 12, 45, 0 );

initializes a new Time object to 12:45 PM and assigns the resulting pointer to timePtr.

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.