I want an array of class objects with unique_ptr
:
std::unique_ptr<MyClass[]> arr(new MyClass[n]);
MyClass
has no default constructor (and in my case is not supposed to have), so I have to put it explicitly here. I cannot find out how to do it so it is syntactically correct. What is the correct way to write a unique_ptr
array of class objects with explicit initialisation?
Clarification
I have a non-default constuctor for MyClass
, like this:
MyClass instance(arguments);
Apart from member initialisations, there are also some calculations in the constructor. I want to create a unique_ptr
array of MyClass
instances and call the constructor for each of the instances. I cannot do that later since MyClass
has no default constructor. Can I put (arguments)
somewhere in std::unique_ptr<MyClass[]> arr(new MyClass[n])
?