C++ Array of pointers: delete or delete []?

后端 未结 8 721
春和景丽
春和景丽 2020-12-04 21:08

Cosider the following code:

class Foo
{
    Monster* monsters[6];

    Foo()
    {
        for (int i = 0; i < 6; i++)
        {
            monsters[i] =         


        
相关标签:
8条回答
  • 2020-12-04 22:02

    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.

    0 讨论(0)
  • 2020-12-04 22:06

    For new you should use delete. For new[] use delete[]. Your second variant is correct.

    0 讨论(0)
提交回复
热议问题