delete[] monsters;
Is incorrect because monsters
isn’t a pointer to a dynamically allocated array, it is an array of pointers. As a class member it will be destroyed automatically when the class instance is destroyed.
Your other implementation is the correct one as the pointers in the array do point to dynamically allocated Monster
objects.
Note that with your current memory allocation strategy you probably want to declare your own copy constructor and copy-assignment operator so that unintentional copying doesn’t cause double deletes. (If you you want to prevent copying you could declare them as private and not actually implement them.)
Related Posts:
- Return array in a function
- Passing a 2D array to a C++ function
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- C++: Expression must have a constant value when declaring array inside function
- Delete 2D array C++
- C++ pass an array by reference
- C++ correct way to return pointer to array from function
- Warning: comparison of distinct pointer types
- Is C++ Array passed by reference or by pointer?
- Array of Linked Lists C++
- Is “delete this” allowed in C++?
- How to dynamically allocate arrays in C++
- How to dynamically allocate arrays in C++
- What are the differences between a pointer variable and a reference variable in C++?
- How do I find the length of an array?
- Initializing an array of objects
- Passing an array by reference
- Regular cast vs. static_cast vs. dynamic_cast
- What does “dereferencing” a pointer mean?
- Meaning of = delete after function declaration
- What is a char*?
- Passing an array by reference
- What exactly is nullptr?
- What is the array form of ‘delete’?
- Use new operator to initialise an array
- What is a dangling pointer?
- What does `*&` in a function declaration mean?
- (->) arrow operator and (.) dot operator , class pointer
- lvalue required as left operand of assignment – Array
- What is uintptr_t data type
- What is the difference between const int*, const int * const, and int const *?
- C++ – No matching member function for call to ‘push_back’
- What is uintptr_t data type
- What does int & mean
- What is the difference between const int*, const int * const, and int const *?
- Is there a function to copy an array in C/C++?
- Cleanest way to copy a constant size array in c++11
- Reverse Contents in Array
- warning: ISO C++ forbids variable length array
- c++ array – expression must have a constant value
- Can’t resolve Error: indirection requires pointer operand (‘int’ invalid)
- Static array vs. dynamic array in C++
- C++ delete vector, objects, free memory
- How to access the contents of a vector from a pointer to the vector in C++?
- Fill array with random numbers within a specified range (C++)
- Remove an array element and shift the remaining ones
- Deleting a dynamically allocated 2D array
- How to add element to C++ array?
- How to find the size of an int[]?
- What does int & mean
- splitting a string into an array in C++ without using vector
- What’s the syntax for declaring an array of function pointers without using a separate typedef?
- Printing an array in C++?
- Sorting Linked List C++ with pointers
- Is the sizeof(some pointer) always equal to four?
- How do you initialise a dynamic array in C++?
- C++ array assign error: invalid array assignment
- C++ Expression must have pointer-to-object type
- C++ strings and pointers
- How to initialize a vector of pointers
- What is the use of intptr_t?
- How to add element to C++ array?
- Reading from .txt file into two dimensional array in c++
- how to initialize an empty integer array in c++
- C++ – Assigning null to a std::string
- Check if C++ Array is Null
- Correct way to work with vector of arrays
- Resizing dynamic array in c++
- C++ Initializing a Global Array
- Comparing the values of char arrays in C++
- Dynamically allocated string array, then change it’s value?
- error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
- Reference to non-static member function must be called
- How can I assign an array from an initializer list?
- What’s the difference between * and & in C?
- creating an array of structs in c++
- Reading data from file into an array
- How to read lines of text from file and put them into an array
- Why can’t we pass arrays to function by value?
- How to memset char array with null terminating character?
- Function stoi not declared
- creating an array of structs in c++
- Why should I use a pointer rather than the object itself?
- too many initializers for ‘int [0]’ c++
- Function stoi not declared
- C++ 2d char array to string
- Using cin for char array
- Initializing pointers in C++
- Using cin to input a single letter into a char
- Different ways to deallocate an array – c++
- Difference between function arguments declared with & and * in C++
- Difference between the int * i and int** i
- set head to NULL (‘NULL’ : undeclared identifier)
- What is a `char*`?
- clearing a vector of pointers [duplicate]
- C++ Swapping Pointers
- Is it still safe to delete nullptr in c++0x?
- Differences between unique_ptr and shared_ptr
- C++ float array initialization