C ++-当向量包含对象时会调用析构函数吗? [英] C++ - Is destructor called when a vector holds objects?
问题描述
如果我在向量内动态分配类的对象,如果使用clear()会为每个对象调用析构函数吗?
If I dynamically allocate objects of a class inside a vector, is the destructor for each object called if I use clear()?
推荐答案
是的,它们都已正确清理。
Yes, they are all cleaned up properly.
来自此链接:
向量的所有元素均被删除:它们的析构函数为调用,然后将其从向量容器中删除,使容器的大小为0。
All the elements of the vector are dropped: their destructors are called, and then they are removed from the vector container, leaving the container with a size of 0.
[sequence.reqmts]
部分也明确了这一点:
The [sequence.reqmts]
section of the upcoming standard also makes this clear:
a.clear()
销毁a
中的所有元素,使所有引用a
并可能使过去的迭代器无效。
a.clear()
destroys all elements ina
, invalidates all references, pointers, and iterators referring to the elements ofa
and may invalidate the past-the-end iterator.
这篇关于C ++-当向量包含对象时会调用析构函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!