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