How does cPP, understand new int [5] ()?

int* some = new int[5]; //
int* ssome = new int[5](); //

if written as new int () [5] , I can understand it this way: generate space in the heap area that can hold five instances of int classes generated by a non-parameterized constructor of type int. On the other hand, `new int [5] only opens up five spaces in the heap where instances of the int class can be stored. Because the former calls the constructor and may initialize a value of 0 in the constructor, the space opened up by the former is already an array of all elements initialized to 0, while the latter has not yet been initialized.

but new int [5] () I can only understand that creating a compound type space on the heap using an int array of length 5 generated by a non-parameter constructor and then the no-parameter constructor of this compound type initializes all the values in the type. That seems to make sense

then I tested

int *test = new int()[5];

well, failed, compilation failed. Maybe there is no no-parameter constructor in the class int, so I am not allowed to write this, so I will create one myself, as follows:

int main(int argc, char** argv) {
    class  test    {
    public:
        test() {};
        ~test() {};
    };
//    test *t = new test()[5]; 
    test *t = new test[5](); //
}

I can"t understand why the first construction method can"t pass, but the second construction method can. I defined the class test from beginning to end, and I haven"t worked on the definition of test [] as a compound type.

and, if the constructor of the test class has and only test (int), then test * t = new test [5] (); compilation fails, test * t = new test [5] (0); compilation fails, test * t = new test (0) [5]; compilation fails.

then my previous understanding of new int [5] () is obviously wrong, so what is the correct understanding?

in addition, for a class that does not have a no-parameter constructor, how can I new multiple of it?

CPP
Dec.21,2021

your first explanation for writing is to build an int array and then initialize all values to 0
. The second is that syntax does not allow compilation and does not pass

.
There's no fundamental reason not to allow a more complicated initializer it's just that CPP03 didn't have a grammar construct for it. In the next version of CPP you will be able to do something like this.
new int [5] {0,1,2,3,4};

you understand correctly that the () section after new int [5] () is indeed used for initialization, but the new operation only allows the use of default constructors with no arguments when creating type arrays. So test * t = new test [5] (0); is not feasible.

new complete integral method:

new [placement] new-type-name [new-initializer]  

placement
overloading new, provides a way to pass additional parameters.

type-name
specifies the type to be assigned; it can be either a built-in type or a user-defined type. If the type specification is complex, you can enclose it in parentheses to enforce the binding order.

initializer
provides a value for the initialization object. You cannot specify an initializer for an array. The new operator creates an array of objects only if the class has a default constructor.

Menu