Is it safe to modify mutable elements of objects inside sets?

I was curious if the following scenario is safe.

I have the following class definitions:

class ActiveStatusEffect
{
public:
    StatusEffect* effect;
    mutable int ReminaingTurns;
    ActiveStatusEffect() : ReminaingTurns(0)
    {
    }
    //Other unimportant stuff down here
}

Then I save a group of them inside std :: set as follows:

struct ASECmp
{
    bool operator ()(const StatusEffects::ActiveStatusEffect &eff1, const StatusEffects::ActiveStatusEffect &eff2)
    {
        return eff1.effect->GetPriority() < eff2.effect->GetPriority();
    }
};
std::set<StatusEffects::ActiveStatusEffect, ASECmp> ActiveStatusEffects;

I mark RemainingTurns as mutable, because I want to be able to change it without dragging, to permanently erase / paste into the set. I.e.

void BaseCharacter::Tick(Battles::BattleField &field, int ticks)
{
    for (auto effect = ActiveStatusEffects.begin(); effect != ActiveStatusEffects.end();)// ++index)
    {
           auto next = effect;
            ++next;
        if (effect->effect->HasFlag(StatusEffects::STATUS_FLAGS::TickEffect) && effect->ReminaingTurns > 0)
        {                       
            effect->effect->TickCharacter(*this, field, ticks);
            --effect->ReminaingTurns;

        }
        if (effect->ReminaingTurns == 0)
        {
            ActiveStatusEffects.erase(effect);
        }
        effect = next;
    }
}

I am worried that for this there may be a problem with ordering within the set, that is, I can not guarantee that the set will always be sorted by effect-> GetPrority ()

If this is true, is there a safe way (for example, to not have RemainingTurns that is part of the key) to do this, in addition to copying, modifying, deleting and pasting what I need to change?

EDIT:

@ildjarn - , , . int, StatusEffect. int .

int StatusEffect::GetPriority() const
{
    return StatusPriority;
}
+5
3

, , , ActiveStatusEffect::ReminaingTurns ActiveStatusEffect, mutable , .

, , , - > GetPrority()

a std::set<StatusEffects::ActiveStatusEffect, ASECmp>; , , ASECmp?

+6

- std:: set, Undefined - , . , " ", , , .

+2

, , :

std::map< int, ActiveStatusEffect > m;
ActiveStatusEffect x = create();
m[ x.effect->GetPriority ] = x;      // !!!

, ( (.. ).

0

All Articles