I am writing a template class which internally manages an array of the given type. Like this:
template<typename T>
class Example {
// ...
private:
T* objects; // allocated in c'tor (array), deleted in d'tor
// ...
};
I was wondering if C++ calls the destructor of each object in objects
when I delete it via delete[] objects;
.
I need to know this, because the objects in my class do not contain sensible values all the time, so the destructors should not be called when they don't.
Additionally, I'd like to know if the destructors would be called if I declared a fixed-sized array like T objects[100]
as part of Example<T>
.
new []
calls constructors it is only logical fordelete []
to call destructors. It is only logical. – Caiaphasstd::vector
instead - you can pre-allocate memory usingreserve
, and then add objects into the vector when you want – Marabou