简体   繁体   English

在c ++中删除指针与对象的向量

[英]deleting a vector of pointers vs. object in c++

I have a vector of pointers to an object (I have them as pointers because I will be swamping the positions around a lot, and I would imagine it would be a lot faster to just swap the pointer, rather than swapping around the whole object. 我有一个指向对象的指针向量(我将它们作为指针,因为我会淹没很多位置,而且我认为交换指针要快得多,而不是交换整个对象。

Anyway, I will ultimately need to delete the vector, but the objects that it points to still need to be valid. 无论如何,我最终需要删除向量,但它指向的对象仍然需要有效。 The documentation seems to say that it will call the destructor on every object in the vector. 文档似乎说它会在向量中的每个对象上调用析构函数。 This makes sense when it's an array of objects, but if the array is an array of pointers to objects, will the objects that the pointers point to also be deleted, or do I need to delete them manually? 当它是一个对象数组时,这是有意义的,但如果数组是一个指向对象的指针数组,那么指针指向的对象是否也会被删除,或者我是否需要手动删除它们?

If they are deleted automatically, is the only way to keep the objects around (say they were used in a different vector) is to actually copy the objects to another location, and have the pointers in the vector point to those objects (rather than the originals)? 如果它们被自动删除,保持对象周围的唯一方法(比如它们在不同的向量中使用)是将对象实际复制到另一个位置,并让向量中的指针指向那些对象(而不是原件)?

Thank you. 谢谢。

Calling a destructor on a pointer value does nothing. 指针值上调用析构函数不会做任何事情。 (On the other hand, calling delete on a pointer value runs the destructor for the pointed-to object, and frees the memory.) (另一方面,在指针值上调用delete运行指向对象的析构函数, 释放内存。)

In the case of an array of pointers to objects, you must free the objects manually if that's what you want. 对于指向对象的指针数组,如果这是您想要的,则必须手动释放对象。

如果你有一个指针向量,如果删除(或清除)向量,实际对象应该仍然存在。

您可以在向量中使用智能指针,例如Boost shared_ptr

It will indeed destruct any objects in the container. 它确实会破坏容器中的任何对象。 However, since all the objects in your container are pointers, that won't do much of anything. 但是,由于容器中的所有对象都是指针,因此不会做任何事情。

Reading your question, that sounds like exactly what you want it to do, so you are good. 阅读你的问题,听起来就像你想要的那样,所以你很好。

It does not matter - that's the reason for the delete keyword. 没关系 - 这就是delete关键字的原因。 If you go out of scope, then the object's destructor is called. 如果超出范围,则调用对象的析构函数。 If a pointer goes out of scope, then it tends to be a memory leak. 如果指针超出范围,那么它往往是内存泄漏。 The same applies here, so you will not have to do anything special. 这同样适用于此,因此您不必做任何特别的事情。

They will continue to exist. 它们将继续存在。

Anyway, I will ultimately need to delete the vector, but the objects that it points to still need to be valid. 无论如何,我最终需要删除向量,但它指向的对象仍然需要有效。 The documentation seems to say that it will call the destructor on every object in the vector. 文档似乎说它会在向量中的每个对象上调用析构函数。 This makes sense when it's an array of objects, but if the array is an array of pointers to objects, will the objects that the pointers point to also be deleted, or do I need to delete them manually? 当它是一个对象数组时,这是有意义的,但如果数组是一个指向对象的指针数组,那么指针指向的对象是否也会被删除,或者我是否需要手动删除它们?

First, read this: http://crazyeddiecpp.blogspot.com/2010/12/pet-peeve.html 首先,请阅读: http//crazyeddiecpp.blogspot.com/2010/12/pet-peeve.html

Now ask yourself, does the documentation say that vector deletes every object that every object it contains points at? 现在问问自己,文档是否说向量删除它包含的每个对象所指向的每个对象?

If you can answer that question with 'No' then there you have it. 如果你能用“否”回答这个问题那么你就有了。

If you can answer that question with 'Yes'...well...try different documentation. 如果你能用“是”回答这个问题......那么......尝试不同的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM