I have a Player
object which can throw an exception inside its constructor, so in my main function I'm creating 2 Player
objects inside a try block.
I want to store these 2 Player
s in a std::array
like that:
try
{
Player p1(10, 10, ikazuchi);
Player p2(70, 10, hibiki);
std::array<Player, 2> players = {p1, p2};
}
The problem is that I wouldn't be able to use the array outside of the try block, and I heard that placing all the main
code inside a try block is often a bad idea.
I can't declare my std::array
after the try block because p1
and p2
no longer exist there.
I can solve the problem with a std::vector
, but I have read that it was better to use a std::array
when I know the size of the array during the compilation.
I could create a default constructor to create my objects then fill them inside the try block, but it seems to be more proper to create everything in the constructor.
What would be the best practice for that?