C ++:在函数中使用当前实例(对象)

时间:2018-05-05 10:05:54

标签: c++ function object instance

问候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;
}

回到帖子问题: 我如何在函数中使用当前对象参数进行计算?

/ *因为我是stackoverflow和C ++的新手感谢所有建议和建议以及评论家! * /

1 个答案:

答案 0 :(得分:0)

在C ++中,您可以将calles作为指针或引用传递

int playerHit(const Player& player, const Ennemy& ennemy)
{
     return ennemy.ehealth - player.pdamage;
};