When you declare the array as test arr[9][9];
then the memory is allocated and default constructor is called for each member. So you don't need call new to allocate new memory.
I assume that you goal is to have array of test objects constructed with the value read from std::cin.
Then you have several options:
- Easiest solution is to use 2D array of pointers:
It would look like:
test* arr[9][9];
for(unsigned int i = 0; i < 9; i++)
{
for(unsigned int j = 0; j < 9; j++)
{
cin >> s[i][j];
arr[i][j] = new test(s[i][j]);
}
}
- If you want to keep the array of plain
test objects (without pointers), the use can either:
Provide a test::set(int) method to set the value after is was constructed.
test arr[9][9];
for(unsigned int i = 0; i < 9; i++)
{
for(unsigned int j = 0; j < 9; j++)
{
cin >> s[i][j];
arr[i][j].set(s[i][j]);
}
}
Construct temporarily object and then assign it to your already allocated one in the array using operator=(const test&) (or operator=(test &&) in c++11). Note that there's no new here:
test arr[9][9];
for(unsigned int i = 0; i < 9; i++)
{
for(unsigned int j = 0; j < 9; j++)
{
cin >> s[i][j];
arr[i][j] = test(s[i][j]);
}
}
Or use placement new (this constructs new object in the pre-allocated memory block):
test arr[9][9];
for(unsigned int i = 0; i < 9; i++)
{
for(unsigned int j = 0; j < 9; j++)
{
cin >> s[i][j];
new(&arr[i][j]) test(s[i][j]);
}
}
- And last one: If you don't have specific reason to use static 2D array, go for some STL container.