If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class
Another option might be to use an array of boost::optional:
boost::optional foos[10]; // No construction takes place
// (similar to vector::reserve)
foos[i] = Foo(3); // Actual construction
One caveat is that you'll have to access the elements with pointer syntax:
bar(*foos[2]); // "bar" is a function taking a "Foo"
std::cout << foos[3]->baz(); // "baz" is a member of "Foo"
You must also be careful not to access an unitialized element.
Another caveat is that this not a true replacement for an array of Foo, as you won't be able to pass it to a function that expects the latter.