jeudi 6 septembre 2018

How to initialize elements of an array managed by a unique_ptr?

We know that Resource Acquisition is Initialization (RAII), I looked for the syntax to initialize an arry of objects that have parameters (with no default params),managed by unique_ptr but I did not found any example, there is one in Cppreference constructing int

int size = 10; 
std::unique_ptr<int[]> fact(new int[size]);

How could I write like this:

 class Widget
{
 Widget(int x, in y):x_(x),y_(y)
{}
int x_,y_;
};

 std::unique_ptr<Widget[]> fact(new Widget[size]);

Aucun commentaire:

Enregistrer un commentaire