scoped_ptr用于双指针

时间:2014-07-16 11:31:27

标签: boost double-pointer scoped-ptr

是否有一种优雅的方式升级到使用boost的scoped_ptr或scoped_array剪切的以下代码?

MyClass** dataPtr = NULL;
dataPtr = new MyClass*[num];
memset(dataPtr, 0, sizeof(MyClass*));
allocateData(dataPtr); // allocates objects under all the pointers

// have fun with the data objects

// now I'm bored and want to get rid of them
for(uint i = 0; i < num; ++i)
  delete dataPtr[i];
delete[] dataPtr;

1 个答案:

答案 0 :(得分:0)

我现在通过以下方式做到了:

boost::scoped_array<MyClass*> dataPtr(new MyClass*[num]);
memset(dataPtr.get(), 0, num * sizeof(MyClass*));
allocateData(dataPtr.get());

似乎工作正常。