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

后端 未结 8 723
春和景丽
春和景丽 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 21:54

    You delete each pointer individually, and then you delete the entire array. Make sure you've defined a proper destructor for the classes being stored in the array, otherwise you cannot be sure that the objects are cleaned up properly. Be sure that all your destructors are virtual so that they behave properly when used with inheritance.

提交回复
热议问题