无论出于何种原因,此参考参数都将返回副本。因此,当我为索引0更改OutWeapon时,它不会影响Weapon1。我做错了吗?

class ULoadout
{
public:
    ULoadout();

    FWeaponSlot Weapon1;
    FWeaponSlot Weapon2;
    FWeaponSlot Weapon3;
    FWeaponSlot Weapon4;

    FSkillSlot Skill1;
    FSkillSlot Skill2;
    FSkillSlot Skill3;
    FSkillSlot Skill4;

    void GetWeapon(int32 InIndex, FWeaponSlot& OutWeapon);
    void GetSkill(int32 InIndex, FSkillSlot& OutSkill);
};

void ULoadout::GetWeapon(int32 InIndex, FWeaponSlot& OutWeapon)
{
    switch (InIndex)
    {
    case 0:
        OutWeapon = Weapon1;
        break;

    case 1:
        OutWeapon = Weapon2;
        break;

    case 2:
        OutWeapon = Weapon3;
        break;

    case 3:
        OutWeapon = Weapon4;
        break;

    default:
        break;
    }
}

最佳答案

重构为

FWeaponSlot& ULoadout::GetWeapon(int32 InIndex)


是明智且惯用的方式。 (C ++不允许您重新绑定引用。)

10-04 14:47