Initializing Objects Created with the new Operator

C++You can initialize objects created with the new operator in several ways. For nonclass objects, or for class objects without constructors, a new initializer expression can be provided in a new expression by specifying ( expression ) or (). For example:

double* pi = new double(3.1415926);
int* score = new int(89);
float* unknown = new float();

If a class does not have a default constructor, the new initializer must be provided when any object of that class is allocated. The arguments of the new initializer must match the arguments of a constructor.

You cannot specify an initializer for arrays. You can initialize an array of class objects only if the class has a default constructor. The constructor is called to initialize each array element (class object).

Initialization using the new initializer is performed only if new successfully allocates storage.

Related References

IBM Copyright 2003