C++ – Empty dynamic array in c++

c++dynamic-arrays

Let's say I have an object called Square with the constructor Square(int rx, int ry), i want to create a dynamic array of Squares, with different arguments in the constructor:

Square *squares = new Square[10];  
for (int i = 0; i < 10; i++)
{
     squares[i] = new Square(i, i);
}

However this fails, saying no appropriate default constructor available. So how do i create an empty or NULL array and then do constructions later?

EDIT: This has to be an array, due to other things in code, to hard to explain here.

Best Solution

Use vector. It doesn't have this problem as long as Square is copyable.

vector<Square> squares;
for (int i = 0; i < 10; i++)
{
     squares.push_back(Square(i, i));
}
Related Question