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;

我现在是这样做的:

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

最新更新