且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

std :: vector是否调用指向对象的指针的析构函数?

更新时间:2022-06-09 23:29:47

否。

如何 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.