我正在使用一个旧的开源库,具有以下(简化的)感兴趣的 API:
// some class that holds a raw pointer to memory on the heap
// DOES NOT delete it in its destructor
// DOES NOT do a "deep" copy when copied/assigned (i.e., after copying both objects
// will point to the same address)
class Point;
// function used to construct a point and allocate its data on the heap
Point AllocPoint();
// function used to release the memory of the point's data
void DeallocPoint(Point& p);
// Receives a pointer/c-array of Points, along with the number of points
// Doesn't own the memory
void Foo(Point* points, int npts);
在 C++11 中使用此 API 的最佳(最安全/最易读/最优雅)的方式是什么。我不能简单地使用vector<unique_ptr<Point, PointDeleter>>
(我可以在哪里PointDeleter
实现一个简单的自定义删除器),因为那样我将无法使用该功能Foo
(期望Point*
而不是unique_ptr<Point>*
)。
谢谢