This must be a valid bit of code.
#include <iostream> #include <list> class Point { public: int x, y; Point(int x1, int y1) { x = x1; y = y1; } }; int main() { std::list<Point> points; points.push_back(Point(0, 0)); points.push_back(Point(1, 1)); points.push_back(Point(2, 2)); std::list<Point>::iterator iter; for(iter = points.begin(); iter != points.end(); ++iter) { Point test = *iter; std::cout << test.x << ", " << test.y << "; "; } std::cout << std::endl; return 0; }
Using this code:
jasons-macbook41:~ g++ test.cpp jasons-macbook41:~ ./a.out 0, 0; 1, 1; 2, 2; jasons-macbook41:~
Although I would not create a temporary copy of the Point as a code. I would rewrite the loop as follows:
for(iter = points.begin(); iter != points.end(); ++iter) { std::cout << iter->x << ", " << iter->y << "; "; }
An iterator is syntactically like a pointer.
EDIT: Given your new problem, drop the โnewโ off the construction line. This creates a pointer to a point, as opposed to a point on the stack. That would be true:
Point* temp = new Point(0, 0);
Or that:
Point temp = Point(0, 0);
And you will be better off with the latter.
source share