我经常需要在C++中处理动态分配的数组,因此需要为scoped_array、shared_array等使用Boost。在阅读了Stroustrup's C++11 FAQ和C++11 Reference Wiki之后,我找不到合适的替代品来替代C++11标准提供的这些动态数组包装器。有没有什么我忽略了的地方,或者我必须继续依赖Boost?
发布于 2011-12-24 13:39:31
像unique_ptr<T[]>
一样,unique_ptr
也有自己的特性。
#include <iostream>
#include <memory>
struct test
{
~test() { std::cout << "test::dtor" << std::endl; }
};
int main()
{
std::unique_ptr<test[]> array(new test[3]);
}
当你运行它的时候,你会得到这样的消息。
test::dtor
test::dtor
test::dtor
如果你想使用shared_ptr
,你应该使用std::default_delete<T[]>
来删除,因为它没有像shared_ptr<t[]>
这样的。
std::shared_ptr<test> array(new test[3], std::default_delete<test[]>());
发布于 2015-02-10 18:12:00
就向量作为数组包装器而言,如果您将任何合适的智能指针与向量作为内部对象一起使用,情况会怎样?
https://stackoverflow.com/questions/8624146
复制相似问题