【发布时间】:2009-02-13 04:20:57
【问题描述】:
我有一个 PolygonList 和一个 Polygon 类型,它们是点列表的 std::lists 或点列表的列表。
class Point {
public:
int x, y;
Point(int x1, int y1)
{
x = x1;
y = y1;
}
};
typedef std::list<Point> Polygon;
typedef std::list<Polygon> PolygonList;
// List of all our polygons
PolygonList polygonList;
但是,我对引用变量和指针感到困惑。
例如,我希望能够引用我的 polygonList 中的第一个 Polygon,并将新的 Point 推送给它。
所以我尝试将多边形列表的前面设置为一个名为 currentPolygon 的多边形,如下所示:
Polygon currentPolygon = polygonList.front();
currentPolygon.push_front(somePoint);
现在,我可以将点添加到 currentPolygon,但这些更改最终不会反映在 polygonList 中的同一个多边形中。 currentPolygon 只是 polygonList 前面的 Polygon 的副本吗?当我稍后遍历 polygonList 时,我添加到 currentPolygon 的所有点都没有显示出来。
如果我这样做,它会起作用:
polygonList.front().push_front(somePoint);
为什么这些不一样?如何创建对物理正面多边形的引用而不是它的副本?
【问题讨论】: