问题描述
code> std :: 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 pointers to objects?
vector<myclass*> stuff;
当内容被销毁时,内部指针指向的单个对象会被破坏吗? p>
When stuff is destroyed, do the individual objects pointed to by the pointers inside stuff get destructed?
推荐答案
否。
如何 std: :vector
应该知道如何销毁指向的对象?是否应该使用 delete
? delete []
? 免费
?一些其他功能?如何知道指向的对象实际上是动态分配的,或者它是一个真正的所有者,并负责销毁它们?
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
是指向对象的One True所有者,使用 std :: unique_ptr
以处理对象的清理。
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是否调用指向对象的指针的析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!