C ++-当向量包含对象时会调用析构函数吗? [英] C++ - Is destructor called when a vector holds objects?

查看:162
本文介绍了C ++-当向量包含对象时会调用析构函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在向量内动态分配类的对象,如果使用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 in a, invalidates all references, pointers, and iterators referring to the elements of a and may invalidate the past-the-end iterator.

这篇关于C ++-当向量包含对象时会调用析构函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆