Compare commits

..

6 commits

View file

@ -122,6 +122,8 @@ struct Asteroid;
struct Spaceship; struct Spaceship;
struct SpaceObject { struct SpaceObject {
virtual ~SpaceObject() = default;
// Only used to kick-start the double-dispatch process // Only used to kick-start the double-dispatch process
virtual void collide_with(SpaceObject& other) = 0; virtual void collide_with(SpaceObject& other) = 0;
@ -130,32 +132,24 @@ struct SpaceObject {
virtual void collide_with(Spaceship& other) = 0; virtual void collide_with(Spaceship& other) = 0;
}; };
struct Asteroid { struct Asteroid : SpaceObject {
void collide_with(SpaceObject& other) override { void collide_with(SpaceObject& other) override {
// `*this` is an `Asteroid&` which kick-starts the double-dispatch // `*this` is an `Asteroid&` which kick-starts the double-dispatch
other.collide_with(*this); other.collide_with(*this);
}; };
void collide_with(Asteroid& other) override { void collide_with(Asteroid& other) override { /* Asteroid/Asteroid */ };
// Asteroid/Asteroid void collide_with(Spaceship& other) override { /* Asteroid/Spaceship */ };
};
void collide_with(Spaceship& other) override {
// Asteroid/Spaceship
};
}; };
struct Spaceship { struct Spaceship : SpaceObject {
void collide_with(SpaceObject& other) override { void collide_with(SpaceObject& other) override {
// `*this` is a `Spaceship&` which kick-starts the double-dispatch // `*this` is a `Spaceship&` which kick-starts the double-dispatch
other.collide_with(*this); other.collide_with(*this);
}; };
void collide_with(Asteroid& other) override { void collide_with(Asteroid& other) override { /* Spaceship/Asteroid */ };
// Spaceship/Asteroid void collide_with(Spaceship& other) override { /* Spaceship/Spaceship */ };
};
void collide_with(Spaceship& other) override {
// Spaceship/Spaceship
};
}; };
void collide(SpaceObject& first, SpaceObject& second) { void collide(SpaceObject& first, SpaceObject& second) {