Assume that a class X
with a constructor functionX(int a, int b)
I am creating a pointer to X as a X *ptr;
dynamic allocation of memory for the class.
Now to create an array of class X object
ptr = new X[sizeOfArray];
everything is still fine. But what I want to do is create the above array of objects that should call the constructor function X(int a, int b)
. I tried the following:
ptr = new X(1,2)[sizeOfArray];
As expected, it gave me a compile-time error
error: expected ';' before '[' token |
How to create an array of objects to call the constructor?
SizeOfArray
entered by the user at runtime.
EDIT:
, , , , . , std::vector
?