C++:在函数中使用当前实例(对象)
C++ : use current instance (object) in function
C++ 社区大家好!
我是 C++ 的新手,我有这个代码示例:
class Player
{
public:
int pdamage;
int phealth;
/* ... other data members and some void member functions (getName etc.) */
};
class Ennemy
{
public:
int edamage;
int ehealth;
};
**/*here i would like to use current objects parameters for calculation and return to current player instance(current player object) the health value.*/**
int playerHit(this.Player.damage,this.Enemy.ehealth)
{
ehealth = this.Ennemy.ehealth - this.Player.damage;
return ehealth;
};
int ennemyHit(this.Player.phealth,this.Enemy.edamage)
{
phealth = this.Player.phealth - this.Ennemy.edamage ;
return ehealth;
};
int main()
{
/*....*/
return 0;
}
回到post问题:
如何在函数中使用当前对象参数进行计算?
/*由于我是 Whosebug 和 C++ 的新手,感谢所有建议和批评! */
在 C++ 中,您可以将调用作为指针或引用传递
int playerHit(const Player& player, const Ennemy& ennemy)
{
return ennemy.ehealth - player.pdamage;
};
C++ 社区大家好!
我是 C++ 的新手,我有这个代码示例:
class Player
{
public:
int pdamage;
int phealth;
/* ... other data members and some void member functions (getName etc.) */
};
class Ennemy
{
public:
int edamage;
int ehealth;
};
**/*here i would like to use current objects parameters for calculation and return to current player instance(current player object) the health value.*/**
int playerHit(this.Player.damage,this.Enemy.ehealth)
{
ehealth = this.Ennemy.ehealth - this.Player.damage;
return ehealth;
};
int ennemyHit(this.Player.phealth,this.Enemy.edamage)
{
phealth = this.Player.phealth - this.Ennemy.edamage ;
return ehealth;
};
int main()
{
/*....*/
return 0;
}
回到post问题: 如何在函数中使用当前对象参数进行计算?
/*由于我是 Whosebug 和 C++ 的新手,感谢所有建议和批评! */
在 C++ 中,您可以将调用作为指针或引用传递
int playerHit(const Player& player, const Ennemy& ennemy)
{
return ennemy.ehealth - player.pdamage;
};