Firstly, cin<<M
is incorrect. It should be cin >> M
Make sure your insert and extract statements point in the right direction.
You cannot with solitary indirection. The new
operator will call the default constructor for each object in the array.
Options to achieve your goal: copy the default value for the desired distribution or create an array of pointers to objects.
Copy method
xyz t(my_other_constructor); xyz* a = new xyz[M]; a[M - 1] = t;
Double indirection
xyz** a = new xyz*[M]; for (int i = 0; i < M - 1; ++i) { a[i] = new xyz; } a[M - 1] = new xyz(my_other_constructor);
Ideally, you would use std::vector
instead of creating hand-held arrays.
source share