【发布时间】:2019-03-22 17:58:55
【问题描述】:
我有一个结构
struct Point
{
int x,y;
Point(int _x,int _y)
{
x=_x,y=_y;
}
int GetX()
{
return x;
}
int GetY()
{
return y;
}
}
如果我打电话在我的程序中
Point *ptr=new Point(5,10);
vector<Point>allpts;
allpts.push_back(Point(ptr->GetX(),ptr->GetY());
这一行之后
ptr 在不应该被推回后被删除。
为什么会这样?
【问题讨论】:
-
ptr is getting deleted after push back when it should not.什么?你怎么知道的? -
为什么使用
new动态分配Point?你接触过 Java 吗?
标签: c++ vector destructor