The programm below is about polymorphism. It just changes the status of a door between open and closed. My problem is that i can't switch the status in my class "Open" to closed since the class "Close" is below my class "Open".
How can i forward declare the class "Close" that it will work properly? (since a normal forward declaration like 'class Close;' doesnt seem to work)
class Status;
class Door {
private:
Status* currentStatus;
public:
void setStatus(Status*);
void opendoor();
void closedoor();
};
class Status {
public:
virtual void opendoor(Door* s)=0;
virtual void closedoor(Door* s) = 0;
};
class Open : public Status {
public:
void opendoor(Door* s) {
return;
}
void closedoor(Door* s){
s->setStatus( new Close() ); //Error here
return;
}
};
class Close : public Status {
public:
void opendoor(Door* s) {
s->setStatus( new Open() );
return;
}
void closedoor(Door* s){
return;
}
};
void Door::opendoor() {
currentStatus->opendoor(this);
return;
}
void Door::closedoor(){
currentStatus->closedoor(this);
return;
}
void Door::setStatus(Status* x) {
delete currentStatus;
currentStatus = x;
return;
}
Aucun commentaire:
Enregistrer un commentaire