jbu*_*jbu 3 c++ memory stack scope
如果我在堆栈上创建一个对象并将其推入列表中,那么该对象将失去作用域(在下面的示例中的for循环之外),该对象是否仍然存在于列表中?如果列表仍然保存对象,那么该数据现在是无效/可能已损坏吗?
请让我知道,请解释理由..
谢谢,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
}
Run Code Online (Sandbox Code Playgroud)
编辑:那如果它是一个std::list<SomeObject*> my_list; 而不是列表...在这种情况下它会无效吗?