关于#c++#的问题:巫师的attrack和cure可以帮助player升级power和lasting,为什么我在主函数里调用巫师技能最后输出显示技能发动前后玩家的power和lasting值不变呢


#include
using namespace std;

enum FAMILY { SUN, MOON, HUMAN };

class Weapon {
private:
    string name;
    int degree;
    float power;
    float lasting;
public:
    Weapon() { power = 0; lasting = 0; degree = 0; }
    Weapon(string _name, float _power = 0, float _lasting = 0, int _degree = 0)
    {
        name = _name;  power = _power; lasting = _lasting; degree = _degree;
    }
    string GetName() { return name; }
    int GetDegree() { return degree; }
    float GetPower() { return power; }
    float GetLasting() { return lasting; }
    void upgrade() { degree++; }
    void uppower(float push) { power += push; }
    void uplasting(float push) { lasting += push; }
    void show()
    {
        cout << "&weapon information:" << endl;
        cout << "name: " << name << endl;
        cout << "degree: " << degree << endl;
        cout << "power: " << power << endl;
        cout << "lasting: " << lasting << endl;
    }
};

class Player {
private:
    string name;
    int experience;
    FAMILY family;
    Weapon weapon;
    static int number;
public:
    friend class Wizard;
    Player() { experience = 0; }
    Player(string _name, Weapon _weapon, int _experience = 0) :weapon(_weapon)
    {
        name = _name; experience = _experience;
        number++;
        family = FAMILY(rand() % 3);
    }
    string getName() { return name; }
    int getExperience() { return experience; }
    FAMILY getFamily() { return family; }
    Weapon getWeapon() { return weapon; }
    Player(const Player& pla)
    {
        name = pla.name;
        experience = pla.experience;
        family = pla.family;
        number++;
    }
    ~Player() { number--; }
    Player& operator=(const Player& pla)
    {
        name = pla.name;
        experience = pla.experience;
        family = pla.family;
        return *this;
    }
    void upgrade(int push)
    {
        experience += push;
        if (push > 50)
            weapon.upgrade();
    }
    void show()
    {
        cout << "=========================================" << endl;
        cout << "**player information:" << endl;
        cout << "name: " << name << endl;
        if (family == 0)
            cout << "family: SUN" << endl;
        else if (family == 1)
            cout << "family: MOON" << endl;
        else
            cout << "family: HUMAN" << endl;
        cout << "experience: " << experience << endl;
        weapon.show();
    }
    void total_num() { cout << "total number of player: " << number << endl; }
};
int Player::number = 0;

class Wizard {
private:
    float attack;
    float cure;
    Player player;
public:
    friend class Player;
    Wizard() { attack = 0; cure = 0; }
    Wizard(Player _player, float _attack = 0, float _cure = 0)
    {
        player = _player; attack = _attack; cure = _cure;
    }
    void upattack() { player.weapon.uppower(attack); }
    void upcure() { player.weapon.uplasting(cure); }
};

int main()
{
    
    //生成3种不同武器
    Weapon weapon1("dagger", 16.6, 23.3, 3);
    Weapon weapon2("axe", 30.4, 20.5, 7);
    Weapon weapon3("sword", 34.6, 41.2, 6);

    //三个玩家,两个巫师
    Player player1("Tom", weapon1, 50);
    player1.show();
    player1.upgrade(20);
    player1.show();
    Player player2("Bob", weapon3, 41);
    player2.show();
    Wizard wizard1(player2, 17.1, 22.9);
    wizard1.upattack();
    player2.show();
    Player player3("Lily", weapon2, 28);
    player3.show();
    Wizard wizard2(player3, 20.7, 11.2);
    wizard2.upcure();
    player3.show();

    Player  ps[3] = { {"Tom", weapon1, 50},{"Bob", weapon3, 41},{"Lily", weapon2, 28} };
    enum FAMILY s;
    int w = 0, e = 0, q = 0;
    for (int i = 0; i < 3; i++)
    {
        s = ps[i].getFamily();
        if (s == 0)
            w++;
        if (s == 1)
            e++;
        if (s == 2)
            q++;
    }
    cout << "SUN:" << w << endl;
    cout << "MOON:" << e << endl;
    cout << "HUMAM:" << q << endl;

    return 0;
}

巫师的attrack和cure可以帮助player升级power和lasting,为什么我在主函数里调用巫师技能最后输出显示技能发动前后玩家的power和lasting值不变呢?

因为你传递的还是它的形参呀,当然修改不了
传递指针进去

class Wizard {
private:
    float attack;
    float cure;
    Player* player;
public:
    friend class Player;
    Wizard() { attack = 0; cure = 0; }
    Wizard(Player* _player, float _attack = 0, float _cure = 0)
    {
        player = _player; attack = _attack; cure = _cure;
    }
    void upattack() { 
        player->weapon.uppower(attack); 
    }
    void upcure() { player->weapon.uplasting(cure); }
};