修改集合内对象的可变成员是否安全? [英] Is it safe to modify mutable members of objects inside sets?
问题描述
我对以下情况是否安全感到好奇.
I was curious as to whether the following scenario is safe.
我有以下类定义:
class ActiveStatusEffect
{
public:
StatusEffect* effect;
mutable int ReminaingTurns;
ActiveStatusEffect() : ReminaingTurns(0)
{
}
//Other unimportant stuff down here
}
然后我将其中的一组存储在std :: set中,如下所示:
I then store a group of these inside an 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;
我将RemainingTurns标记为可变的,因为我希望能够更改它而不必不断擦除/插入到集合中.即
I mark RemainingTurns as mutable because I want to be able to change it without haing to constantly erase/insert 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;
}
}
我很担心,因为这似乎有可能弄乱集合中的顺序,这意味着我不能保证集合将始终按effect-> GetPrority()
I'm concerned because it seems possible for this to mess up the ordering within the set, meaning I can't guarantee the set will always be sorted by effect->GetPrority()
如果是这样,除了复制,修改,擦除然后插入我需要更改的内容之外,是否有安全的方法(例如没有RemainingTurns构成键的一部分)?
If that's true, is there a safe way (such as not have RemainingTurns form part of the key) to do this besides copying, modifying, erasing then inserting what I need to change?
@ildjarn-抱歉,我认为没关系.它只是返回一个存储在StatusEffect中的int值.保证int在程序运行时不会改变.
@ildjarn - sorry, I didn't think that mattered. It just returns an int stored within StatusEffect. That int is guaranteed not to change over the runtime of the program.
int StatusEffect::GetPriority() const
{
return StatusPriority;
}
推荐答案
更改影响对象顺序的数据确实会破坏关联容器的不变性,但是因为 ActiveStatusEffect :: ReminaingTurns
并非如此涉及到 ActiveStatusEffect
对象的排序,将其保持为 mutable
并修改其值是完全无害的.
Changing data that affects the ordering of an object will indeed break the invariants of associative containers, but because ActiveStatusEffect::ReminaingTurns
is not involved in the ordering of ActiveStatusEffect
objects whatsoever, keeping it mutable
and modifying its value is perfectly harmless.
我很担心,因为这似乎有可能弄乱集合中的顺序,这意味着我不能保证集合将始终按effect-> GetPrority()
I'm concerned because it seems possible for this to mess up the ordering within the set, meaning I can't guarantee the set will always be sorted by effect->GetPrority()
这是 std :: set< StatusEffects :: ActiveStatusEffect,ASECmp>
;除了 ASECmp
定义的标准以外,如何按其他标准进行排序?
It's a std::set<StatusEffects::ActiveStatusEffect, ASECmp>
; how could it sort by any criteria other than that defined by ASECmp
?
这篇关于修改集合内对象的可变成员是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!