Do not understand conditional in for loop
Hello, I was hoping someone could help me understand the conditional in this for loop:
Code:
for (auto i = maps.begin(); i != maps.end(); i++)
{
if (&((*i).second) == this)
{
assert(!getManager().hasActiveMap());
ret = ret && getManager().setActiveMap((*i).first);
}
}
what is happening here:
Code:
if (&((*i).second) == this)
is this just asking for the value located at "second" of the index i? If so, could this not just be:
?
Re: Do not understand conditional in for loop
Quote:
Originally Posted by
Circuits
what is happening here:
Code:
if (&((*i).second) == this)
is this just asking for the value located at "second" of the index i? If so, could this not just be:
?
No.
just because is the same asBesides it is in the most cases easier to use construction like
Code:
for (auto& it : maps)
rather than
Code:
for (auto i = maps.begin(); i != maps.end(); i++)
Re: Do not understand conditional in for loop
You need to consider precedence. . has the highest level and then & (as address). and * (as de-reference). So to have .second you need (*I).
You could have:
Code:
if (&i->second == this)
as -> has a higher precedence than &
Re: Do not understand conditional in for loop
Would this be equivalent then?
Code:
for (auto&& i : maps)
{
if (i.second == this)
{
assert(!getManager().hasActiveMap());
ret = ret && getManager().setActiveMap((*i).first);
}
}
Re: Do not understand conditional in for loop
Quote:
Originally Posted by
Circuits
Would this be equivalent then?
Code:
for (auto&& i : maps)
{
if (i.second == this)
{
assert(!getManager().hasActiveMap());
ret = ret && getManager().setActiveMap((*i).first);
}
}
Does it compile???
Re: Do not understand conditional in for loop
Oh, heh no, no it doesn't :d
Code:
for (auto& i : maps)
{
if (&i.second == this)
{
assert(!getManager().hasActiveMap());
ret = ret && getManager().setActiveMap((i).first);
}
}
Re: Do not understand conditional in for loop
Quote:
Originally Posted by
Circuits
Oh, heh no, no it doesn't :d
It doesn't compile? And what the compiler says?