C++ std::vector 是否调用对象指针的析构函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9448260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does std::vector call the destructor of pointers to objects?
提问by James McNellis
Possible Duplicate:
Deleting pointers in a vector
可能的重复:
删除向量中的指针
I know when an std::vector
is destructed, it will call the destructor of each of its items. Does it call the destructor of pointersto objects?
我知道当一个std::vector
被破坏时,它会调用它的每个项目的析构函数。它是否调用对象指针的析构函数?
vector<myclass*> stuff;
When stuff is destroyed, do the individual objects pointed to by the pointers inside stuff get destructed?
当东西被销毁时,东西内部的指针指向的单个对象会被销毁吗?
回答by James McNellis
No.
不。
How is std::vector
supposed to know how to destroy the pointed-to object? Should it use delete
? delete[]
? free
? Some other function? How is it supposed to know the pointed-to objects are actually dynamically allocated or that it is the One True Owner and is responsible for destroying them?
std::vector
应该如何知道如何销毁指向的对象?它应该使用delete
吗? delete[]
? free
? 还有什么功能?它如何知道指向的对象实际上是动态分配的,或者它是唯一真正的所有者并负责销毁它们?
If the std::vector
is the One True Owner of the pointed-to objects, use std::unique_ptr
, potentially with a custom deleter to handle the cleanup of the objects.
如果std::vector
是指向对象的唯一真正所有者,则使用std::unique_ptr
,可能与自定义删除器一起处理对象的清理。
回答by AnT
As you said correctly yourself, vector does call destructors for its elements. So, in your example the vector doescall "destructors of pointers". However, you have to keep in mind that pointer types have no destructors. Only class types can have destructors. And pointers are not classes. So, it is more correct to say that std::vector
applies the pseudo-destructor call syntax to the pointer objects stored in the vector. For pointer types that results in no-operation, i.e it does nothing.
正如您自己所说的那样,vector 确实为其元素调用了析构函数。因此,在您的示例中,向量确实调用了“指针的析构函数”。但是,您必须记住指针类型没有析构函数。只有类类型可以有析构函数。指针不是类。因此,更正确的说法是std::vector
将伪析构函数调用语法应用于存储在向量中的指针对象。对于导致无操作的指针类型,即它什么都不做。
This also answers the second part of your question: whether the myclass
objects pointed by the pointers get destroyed. No, they don't get destroyed.
这也回答了您问题的第二部分:myclass
指针指向的对象是否被销毁。不,它们不会被摧毁。
Also, it seems that you somehow believe that "calling destructors on pointers" (the first part of your question) is the same thing as "destroying the pointed objects" (the second part of your question). In reality these are two completely different unrelated things.
此外,您似乎以某种方式相信“在指针上调用析构函数”(问题的第一部分)与“销毁尖头对象”(问题的第二部分)是一回事。实际上,这是两个完全不同的不相关的事情。
In order to create a link from the former to the latter, i.e. to make the vector destroy the pointed objects, you need to build your vector from some sort of "smart pointers", as opposed to ordinary raw myclass *
pointers. The vector will automatically call the destructors of the "smart pointers" and these destructors, in turn, will destroy the pointed objects. This "link" can only be implemented explicitly, inside the "smart pointer's" destructor, which is why ordinary raw pointers can't help you here.
为了创建从前者到后者的链接,即让向量破坏指向的对象,您需要从某种“智能指针”构建向量,而不是普通的原始myclass *
指针。向量将自动调用“智能指针”的析构函数,这些析构函数依次销毁指向的对象。这个“链接”只能在“智能指针”的析构函数中显式实现,这就是为什么普通的原始指针在这里无法帮助你。
回答by Seth Carnegie
No; what if you stored a pointer to an automatic object?
不; 如果你存储了一个指向自动对象的指针呢?
vector<T*> v;
T tinst;
v.push_back(&tinst);
If the vector calls the destructors of the objects the pointers point to, the automatic object would be destructed twice - once when it went out of scope, and once when the vector went out of scope. Also, what if they are not supposed to be deallocated with delete
? There's no way it could behave appropriately in every situation.
如果向量调用指针指向的对象的析构函数,则自动对象将被破坏两次 - 一次超出范围,一次是向量超出范围。另外,如果它们不应该被解除分配delete
怎么办?它不可能在每种情况下都表现得很好。
If your objects are all allocated dynamically, you have to manually iterate the vector and delete
each pointer if it was allocated with new
. Alternatively, you can create a vector of smart pointers which willdeallocate the objects pointed to by the pointers:
如果你的对象都是动态分配的,你必须手动迭代向量和delete
每个指针(如果它是用new
. 或者,您可以创建一个智能指针向量,它将释放指针指向的对象:
vector<shared_ptr<T>> v;
v.push_back(new T);