哪种类型的智能指针(共享,作用域)最适合这种数据结构......
结构一:
//Class with cross-references to points p1, p2
class PointTopo
{
private:
double x, y;
PointTopo * p1;
PointTopo * p2;
public:
PointTopo(double xx, double yy): x(xx), y(yy) {this-> p1 = NULL; this->p2 = NULL;}
...
};
结构二:
//Class with cross references: topological model for Delaunay triangulation
class Edge
{
private:
Point * start; //Only 2D point without topo information
Edge *next;
Edge *previous;
Edge *twin;
...
};
我想使用向量存储 Edges 和 PointTopo:
class PointsTopoList
{
private:
std::vector <PointTopo *> points;
public:
inline void push_back ( PointTopo *p ) { points.push_back ( p );}
~PointsTopoList() {clear();}
void clear()
{
for ( TNodes2DList::iterator i_points= points.begin(); i_points!= points.end(); ++i_points)
{
if ( *i_points!= NULL )
{
delete *i_points;
*i_points= NULL;
}
points.clear();
}
}
但是析构函数存在问题,所以我想知道是否使用引用计数。
int main()
{
PointTopo *p1 = new PointTopo(0,0);
PointTopo *p2 = new PointTopo(10,10);
PointTopo *p3 = new PointTopo(20,20);
PointTopo *p4 = new PointTopo(30,30);
PointsTopoList tl1;
tl1.push_back(p1);
tl1.push_back(p2);
tl1.push_back(p3);
tl1.push_back(p4);
PointsTopoList tl2;
tl2.push_back(p1); //P1 is stored in tl1 and tl2
tl2.push_back(p2); //P2 is stored in tl1 and tl2
}
点 p1、p2 将存储在两个列表 tl1、tl2 中。tl2 的析构函数导致异常,点 p1 和 p2 已使用 tl1 析构函数删除。
这个例子不是合成的。想象一下,nl2 代表 nl1 的子集,例如 nl1 的凸包......
我认为,如果没有引用计数就无法解决这个问题......所以我尝试使用一些智能指针......
非常感谢您的帮助...