我正在尝试学习C ++ OOP,我制作了以下代码:
#include <iostream>
#include <string>
#include "monster.h"
using namespace std;
int main(int argc, char** argv) {
Monster monster("Wizard",150,50);
Monster monster2("Gorgoyle",450,15);
cout << monster2.getHealth() << endl;
monster.attack(monster2);
cout << monster2.getHealth() << endl;
}
#ifndef MONSTER_H
#define MONSTER_H
#include <iostream>
#include <string>
using namespace std;
class Monster
{
public:
Monster(string name_, int health_, int damage_);
~Monster();
int attack(Monster opponet);
int getHealth();
string name;
int damage;
int health = 0;
int getDamage();
void setHealth(int health_);
void setDamage(int damage_);
void setName(string name);
void doDamageToOpponent(Monster opponent);
string getName();
};
#endif
#include "monster.h"
Monster::Monster(string name_, int health_, int damage_) {
health = health_;
setDamage(damage_);
setName(name_);
}
Monster::~Monster() { }
int Monster::attack(Monster opponent) {
doDamageToOpponent(opponent);
}
void Monster::doDamageToOpponent(Monster opponent) {
int newHealth = opponent.getHealth() - this->getDamage();
opponent.setHealth(newHealth);
}
int Monster::getHealth() {
return health;
}
int Monster::getDamage() {
return damage;
}
void Monster::setHealth(int health_) {
health = health_;
}
void Monster::setDamage(int damage_) {
this->damage = damage_;
}
void Monster::setName(string name_) {
this->name = name_;
}
string Monster::getName() {
return name;
}
现在我的问题是,当我运行这段代码时,我希望有一个monster2对象可以保留400个生命值,但它仍然是450:S
为了达到这个目的,必须做些什么?我注意到它在doDamageToOppoenet中可以是400,但是当它离开那个块时,它仍然是450.请帮助我!感谢。
答案 0 :(得分:2)
您按价值传递对象:
void Monster::doDamageToOpponent(Monster opponent) <- This should be by reference
int Monster::attack(Monster opponent) <- idem
这意味着:您正在创建Monster
对象的新副本,以便在您正在调用的函数中造成损害,然后实际处理该副本损坏但保留原来旧具有未触及值的对象。
以下签名将起作用:
void Monster::doDamageToOpponent(Monster& opponent)
int Monster::attack(Monster& opponent)
如果您想了解更多相关信息,请阅读以下内容:Passing stuff by reference和Passing stuff by value
答案 1 :(得分:2)
原因是函数attack
和doDamageToOpponent
正在获取参数的副本,因为您通过值传递 。然后发生的事情是你改变函数内传递的Monsters的副本。函数返回后,这些副本会死亡(因为它们是函数的本地副本)并且原始的感兴趣的各方都没有发生任何事情。
尝试通过引用传递参数。引用就好像它是原始变量一样。考虑:
int a = 0;
int &refa = a; /* refa acts as real "a", it refers to the same object "a" */
int b = a; /* this is your case */
b = 6; /* b will be changed, but "a" not */
refa = 6; /* a is changed, really "a", refa is just different name for "a" */
尝试:
int Monster::attack( Monster &opponent){
doDamageToOpponent( opponent);
}
void Monster::doDamageToOpponent( Monster &opponent){
int newHealth = opponent.getHealth() - this->getDamage();
opponent.setHealth( newHealth);
}
答案 2 :(得分:1)
你正在通过值传递对手,即功能:
int Monster::attack(Monster opponent);
实际上会收到对手的副本并修改该副本。每次有一个修改某个对象的函数时,你需要通过引用传递要修改的对象,或者将指针传递给它,例如,
int Monster::attack(Monster& opponent);
或
int Monster::attack(Monster* opponent);
我建议使用const T&
作为输入参数,使用T*
作为输出参数,所以在这种情况下,后一种形式。我推荐后者用于输出参数的原因是因为它使调用者更明确:
monster.attack(&monster2); // passing a pointer: monster2 will be modified.