Changing the object a pointer points to in c++

If hero is a pointer you need decide whether to compare the heroes (eg. by name with overloaded operators) or the adresses. So it should be:

if(*activeHero==hero[0])
    {
        *activeHero  =hero[1];
        hero[1]->setFocus();
    }
    else if(*activeHero == hero[1])
    {
        *activeHero = hero[0];
        hero[0]->setFocus();
    }

or it should be

if(activeHero==&hero[0])//address of hero[0]
{
    activeHero  =&hero[1];
    hero[1]->setFocus();
}
else if(activeHero == &hero[1])
{
    activeHero = &hero[0];
    hero[0]->setFocus();
}

Leave a Comment