I'm using std::array to replace the 'pointer and count' calling methods in some C code I'm upgrading to C++. Creating and using the std::array is no problem, but on destruction it will call the destructor on all elements.
But my arrays are often not completely full, causing a destructor call on an empty element. Usually not a problem, as they will be constructed with the default constructor, but I'd like to just remove an element by decrementing a size count, which means leaving a 'used' element in the array, which then causes problems on destruction.
What would be the best way to make sure only a part of the array is destroyed, by having the destructor called on its elements?
Sorry, I haven't been clear. I created a class that contains a std::array and a size int.
class MyArray {
std::array< myclass, N > m_array;
unsigned int m_size;
};
The array will not always be full, obviously. I need a destructor that will only destroy the first m_size elements of m_array.
Would this work?
MyArray::~MyArray() {
for (unsigned int s = 0; s < m_size; ++s) {
m_array[s].~myclass();
}
}
My fear is the destructor being called twice on myclass.