Given the following pseudo code I'm trying to write:
#define ANIMAL_NUMBER 10
#define MALE 1
#define FEMALE 0
class Zoo
{
public:
Animal* GetAnimal(int id) {return &animal[id];}
private:
Animal animal[ANIMAL_NUMBER];
};
class Animal
{
public:
Animal():position_(rand()%2),speed_(rand()%100),life(100){}; //ctor
void Kill(Animal* animal)
{
animal->life_ = 0;
};
void KillSlowMaleAnimalsInZoo(void)
{
for(int id=0; id < ANIMAL_NUMBER; id++)
{
Animal* animal = Zoo::GetAnimal(id);
if((animal->life_ > 0) &&
(animal->sex_ == MALE) &&
(animal->GetSpeed()<20))
{
Kill(animal);
}
};
int GetSpeed(void)
{
return speed_;
};
private:
Sex sex_;
int speed_;
int life_;
};
Is there a specific design pattern to answer this particular problem?
What I want is to access another class Animal instance in the Animal class definition itself.
Aucun commentaire:
Enregistrer un commentaire