Assume a class X
with a constructor function X(int a, int b)
I create a pointer to X as X *ptr;
to allocate memory dynamically for the class.
Now to create an array of object of class X
ptr = new X[sizeOfArray];
until now everything is fine. But what I want to do is creation of the above array of objects should invoke the constructor function X(int a, int b)
. I tried as follows:
ptr = new X(1,2)[sizeOfArray];
As expected It gave me compile time error
error: expected ';' before '[' token|
How can I create an array of objects to invoke the constructor?
SizeOfArray
is entered by the user at runtime.
EDIT:
What I wanted to achieve in not possible as answered by zenith or will be too complex . So how can I use std::vector
for the same?