I have these classes and I want them to communicate without using global variables. This is how I solved it.
Code:
#include <iostream>
#include <vector>
class Player {
private:
unsigned short hp;
public:
void damage();
Player(): hp{100} {}
};
void Player::damage() {
--hp;
}
class Enemy {
protected:
unsigned short hp;
public:
virtual void update(Player&)=0;
Enemy(): hp{} {}
virtual ~Enemy(){}
};
class Bat: public Enemy {
public:
void update(Player&);
};
void Bat::update(Player& player) {
player.damage();
}
int main() {
Player player;
std::vector<Enemy*> enemies;
enemies.push_back(new Bat);
for(unsigned int i{}; i<enemies.size(); ++i)
enemies[i]->update(player);
delete enemies[0];
}
Is there a smarter design decision than this? The crucial part beeing having to pass player to void Bat::update(Player& player).
I also wrote a Player class with static members and friendship with Enemy but that didn't include Bat for some reason.
Any suggestions would be helpful, thanks.