如果我在堆栈上创建一个对象并将其推入列表中,那么对象失去作用域(在下面示例中的for循环之外),该对象是否仍然存在于列表中?如果列表仍然持有对象,那么数据现在是否无效/可能损坏?
请告诉我,并解释原因。
谢谢, jbu
请告诉我,并解释原因。
谢谢, jbu
class SomeObject{
public:
AnotherObject x;
}
//And then...
void someMethod()
{
std::list<SomeObject> my_list;
for(int i = 0; i < SOME_NUMBER; i++)
{
SomeObject tmp;
my_list.push_back(tmp);
//after the for loop iteration, tmp loses scope
}
my_list.front(); //at this point will my_list be full of valid SomeObjects or will the SomeObjects no longer be valid, even if they still point to dirty data
}
编辑:如果它是一个std::list<SomeObject*> my_list
而不是list...那么这种情况下是否无效?