BOOST_FOREACH使位於GroupMember類中的成員weak_ptr無效,請幫助我理解原因。在BOOST_FOREACH循環內設置的變量在循環外無效,爲什麼?
下面的代碼說明錯誤:
class GroupMember
{
bool logInState;
boost::weak_ptr<CUser> wpUser;
};
GroupMember::iterator it;
BOOST_FOREACH(EachLevel aLevel, levels)
{
if(aLevel.exist(spUser))
{
it = aLevel.getIteratorToGroupMember(spUser);
//iterator (it) is valid as well as the group member's attributes (and weak_ptr)
}
}
//Iterator (it) seems to be valid but the weak_ptr is invalid.
//The counter to the object is more than 10 so the weak ptr is not expired.
下面的代碼工作完美:
GroupMember::iterator it;
std::vector<EachLevel>::iterator itLevel;
for(itLevel = levels.begin(); itLevel != levels.end(); ++itLevel)
{
if(itLevel->exist(spUser))
it = itLevel->getIteratorToGroupMember(spUser);
}
//Here is iterator (it) valid (including the weak_ptr)
我看不出差別,你能嗎?
謝謝!
啊,我明白了!它與增加的參考一起工作。 – user521048 2010-11-26 08:44:50