我有一类名为Group
:对象更新
class Group
{
int id;
string addr;
set<int> members;
...
};
我有一个指针存储在这些容器多个组:
vector<Group*> grpVec
map<int, Group*> grpIdMap
map<string, Group*> grpAdMap
我保存这样的指针:
//create and populate group object pointer Group *grp
grpVec.push_back(grp)
grpIdMap.insert(std::pair<int,Group*>(grp->id, grp))
grpAdMap.insert(std::pair<string,Group*>(grp->addr, grp))
现在,我想更新一个组对象。如果我只更新grpIdMap容器中的指针,所有其他容器中的相同对象指针是否会更新?
//Will this update the same pointer object in grpVec and grpAdMap?
grpIdMap.find(1)->second->members.insert(99)
这种方法有什么问题吗?
看起来你应该使用共享指针,即'std :: shared_ptr'而不是原始指针。 –
PaulMcKenzie
所有这些指针,实际存储的对象是什么? –
这是你的方法问题:如果'grp'是动态分配的,哪个容器负责释放内存? – PaulMcKenzie