某段的函数功能为,从数据库中读取数据,存入结构体A的数组中,然后循环数组,用结构体A给结构体B赋值,用来达到数据结构升级的目的
std::vector<A> structA;
// 从数据库中给structA赋值
std::vector<B> structB;
for(auto item = structA.begin();item != struct.end();++item)
{
B temp;
temp.id = item.id;
……
structB.push_back(temp);
}
然后在函数体结束后就出现了这种问题
查了很多文章都没有解决问题,从某篇文章得到灵感,提到了是循环里的临时结构体的问题
解决方案:将循环里的临时变量定义成指针就可以了
例如:
std::vector<A> structA;
// 从数据库中给structA赋值
std::vector<B> structB;
for(auto item = structA.begin();item != struct.end();++item)
{
B* temp = new B();
temp->id = item.id;
……
structB.push_back(*temp);
}
具体原因还望各位大佬能够答疑一下