void arrayList<T>::erase(int theIndex) {
// valid index, shift elements with higher index
copy(element + theIndex + 1, element + listSize, element + theIndex);
element[--listSize].~T(); // invoke destructor
}
代码 element[--listSize].~T()
看起来很奇怪。它用于删除动态分配的元素。有谁知道为什么我们可以使用这种语法?
编辑
问题解决了。
最佳答案
如果您使用placement new
运算符为elements[i]
分配了内存,这将很有用。参见 What is "placement new" and why would I use it?了解详情。
关于C++ 析构函数格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4877998/