Now,I hava a class with some components to do things while these components also need the reference of this parent class to update states.They coupling with each other make the design hard.Here is a example code snippet:
#include<iostream>
#include <windows.h>
#include <math.h>
using namespace std;
class man {
public:
int health{ 10 }, x, y;
void update();
private:
class moveCom *_move;
class judgeCom *_judge;
};
class moveCom {
public:
void update(man&m) {
++m.x;
++m.y;
}
};
class judgeCom {
public:
void update(man&m) {
if(rand()%10 <= 5)
++m.health;
else --m.health;
}
};
void man::update() {
_move->update(*this);
_judge->update(*this);
cout << health << " " << x << " " << y << endl;
}
int main() {
man m;
while (1) {
Sleep(200);
m.update();
}
return 0;
}
This code can only work in the same cpp file which means if split them into different hpp/cpp will cause compile error.Is there any practice to do better?
Aucun commentaire:
Enregistrer un commentaire