Cosider the following code:
class Foo
{
Monster* monsters[6];
Foo()
{
for (int i = 0; i < 6; i++)
{
monsters[i] =
The second one is correct under the circumstances (well, the least wrong, anyway).
Edit: "least wrong", as in the original code shows no good reason to be using new
or delete
in the first place, so you should probably just use:
std::vector<Monster> monsters;
The result will be simpler code and cleaner separation of responsibilities.
For new
you should use delete
. For new[]
use delete[]
. Your second variant is correct.