I am curious about new int[n]()
operator, as per my knowledge it uses to initialize the array values to zero which is created using the new
operator. But I can't use some value in parenthesis like new int[n](x)
to initialize values to x
, why I can't do that, What happens at the compiler level when I use new int[n]()
?
int
is a POD and per C++11 you value initialize the array. In the case of int
it is 0
.
The reason it doesn't work with value in parantheses is because it is not defined. If you insist (for educational reason) you can overload the new operator to do what you want.
You can initialize the array with initializer list though:
int* a = new int[10]{10, 6};
However it will initialize the first two elements in this example.