我正在尝试删除旧应用程序的所有删除和删除[],而是使用智能指针.在下面的代码片段中,我想删除cicle的最后一个.
std::unique_ptr<MapiFileDesc> fileDesc(new MapiFileDesc[numFiles]);
for (int i = 0; i < numFiles; ++i)
{
// Works but I've to delete[] at the end
fileDesc[i].lpszPathName = new CHAR[MAX_PATH];
// Does not work. For each iteration the previous array will be deleted
// It also happens with shared_array
boost::scoped_array<CHAR> pathName(new CHAR[MAX_PATH]);
fileDesc[i].lpszPathName = pathName.get();
}
// I want to remove the following cicle
for (int i = 0; i < numFiles; ++i)
{
delete [] fileDesc[i].lpszPathName;
fileDesc[i].lpszPathName = nullptr;
}
Run Code Online (Sandbox Code Playgroud)
您认为这种情况的最佳方法是什么:使用包装器对象来跟踪所有创建的数组并在析构函数中删除它们或使用boost :: …