Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How am I accidentally overwriting when referencing these pointers?

Tags:

c++

pointers

Last question for tonight, I promise. These pointers are giving me a serious headache.

I have a std::list<Point> called Polygon and a std::list of Polygons defined like:

typedef std::list<Point> Polygon; 
typedef std::list<Polygon> PolygonList; 

// List of all our polygons 
PolygonList polygonList; 

I created the method below to attempt to delete the nearest Point from an (x,y), checking all of my Polygons within my polygonList.

void deleteNearestPoint(int x, int y)
{
    y = screenHeight - y;

    Polygon &closestPolygon = polygonList.front();
    Polygon::iterator closestPoint = closestPolygon.begin();

    float closestDistance = sqrt(pow(x - closestPoint->x, 2) + pow(y - closestPoint->y, 2));

    // Search PolygonList
    PolygonList::iterator listIter;
    Polygon::iterator iter;

    for(listIter = polygonList.begin(); listIter != polygonList.end(); listIter++)
    {
        Polygon &tempPolygon = *listIter;

        for(iter = tempPolygon.begin(); iter != tempPolygon.end(); iter++)
        {
            const float distance = sqrt(pow(x - iter->x, 2) + pow(y - iter->y, 2));

            if (distance < closestDistance)
            {
                closestPolygon = *listIter;
                closestPoint = iter;
                closestDistance = distance;
            }
        }

    }

    closestPolygon.erase(closestPoint);

    redraw();
}

However, somewhere I have a pointer or reference variable screwing me up. This code compiles but acts in a very strange way.

I've written a debug statement and lets say I have 3 polygons in my polygon list like so:

Polygon #: 0
Point: (448, 43)
Point: (469, 177)
Point: (374, 123)
Polygon #: 1
Point: (295, 360)
Point: (422, 350)
Point: (315, 266)
Point: (295, 360)
Polygon #: 2
Point: (143, 202)
Point: (301, 203)
Point: (222, 100)
Point: (143, 202)

Now, lets say I try and use the delete function giving it an x/y close to point 422, 350 The desired result would be it simply deleting that point (422, 350) from Polygon #1 but instead I get this:

Polygon #: 0
Point: (295, 360)
Point: (422, 350)
Point: (315, 266)
Point: (295, 360)
Polygon #: 1
Point: (295, 360)
Point: (315, 266)
Point: (295, 360)
Polygon #: 2
Point: (143, 202)
Point: (301, 203)
Point: (222, 100)
Point: (143, 202)

It did delete (422, 350) but it also has the strange side effect of overwriting Polygon#0 to what Polygon#1 was before the delete of its point.

I know I'm using a pointer or reference incorrectly in my method. Can someone point out what I could possibly be doing that is causing this? I think it is because my &closestPolygon is declared as a reference, but I get compile errors if I try to set it as anything else.

like image 708
KingNestor Avatar asked Feb 13 '09 05:02

KingNestor


1 Answers

Unfortunately, you cannot rebind a reference, i.e., this line:

closestPolygon = *listIter;

will copy *listIter to closestPolygon, not rebind the refernece to *listIter.

Edit: To do what you want, you should use PolygonList::iterator instead of Polygon & and adjust the code accordingly.

like image 193
MSN Avatar answered Sep 26 '22 08:09

MSN