我是新來的C++,發現它混淆了stl容器上的處理指針。 stl容器如何處理指針?STL容器上的指針
Point *p1 = new Point(10, 10);
std::vector<Point*> points;
points.push_back(p1);
delete p1; // or delete points[0]
std::cout << points[0]->getID() << "\n"; //why does this still display 10, 10 after deleting above?
std::cout << p1->getID(); //ofcourse, this one will output garbage
//getID method displays xy coordinates given as parameters when object is created
//The result displayed
10, 10
-1, 12337
簡單:未定義的行爲。 – chris 2013-02-25 06:37:16
我懷疑你是否需要指針。是否有一個原因,你爲什麼不「按價值」放在你的矢量點? – leemes 2013-02-25 06:43:07