Skip to content

Instantly share code, notes, and snippets.

@matovitch
Last active August 29, 2015 14:20
Show Gist options
  • Save matovitch/3dad7e9092dec9427c79 to your computer and use it in GitHub Desktop.
Save matovitch/3dad7e9092dec9427c79 to your computer and use it in GitHub Desktop.
#include "visitor.hpp"
#include <iostream>
class OtherTypeAsteroid;
class ExplodingAsteroid;
class ApolloSpacecraft;
class OthersSpacecraft;
class SpaceShip {};
class Asteroid : public Visitor<ApolloSpacecraft, OthersSpacecraft>{};
class Alien : public Visitor<ApolloSpacecraft, OthersSpacecraft>{};
struct OthersSpacecraft : public SpaceShip, public Visitable<OthersSpacecraft, Asteroid, Alien> {};
struct ApolloSpacecraft : public SpaceShip, public Visitable<ApolloSpacecraft, Asteroid, Alien> {};
class ExplodingAsteroid : public Asteroid {
public:
virtual void visit(OthersSpacecraft&) {
std::cout << "ExplodingAsteroid hit a OthersSpacecraft" << std::endl;
}
virtual void visit(ApolloSpacecraft&) {
std::cout << "ExplodingAsteroid hit an ApolloSpacecraft" << std::endl;
}
};
class OtherTypeAsteroid : public Asteroid {
public:
virtual void visit(OthersSpacecraft&) {
std::cout << "OtherTypeAsteroid hit a OthersSpacecraft" << std::endl;
}
virtual void visit(ApolloSpacecraft&) {
std::cout << "OtherTypeAsteroid hit an ApolloSpacecraft" << std::endl;
}
};
struct AlienA : public Alien
{
virtual void visit(OthersSpacecraft&) {
std::cout << "AlienA hit a OthersSpacecraft" << std::endl;
}
virtual void visit(ApolloSpacecraft&) {
std::cout << "AlienA hit an ApolloSpacecraft" << std::endl;
}
};
struct AlienB : public Alien
{
virtual void visit(OthersSpacecraft&) {
std::cout << "AlienB hit a OthersSpacecraft" << std::endl;
}
virtual void visit(ApolloSpacecraft&) {
std::cout << "AlienB hit an ApolloSpacecraft" << std::endl;
}
};
int main()
{
OthersSpacecraft theOthersSpacecraft;
ApolloSpacecraft theApolloSpacecraft;
OtherTypeAsteroid theOtherTypeAsteroid;
ExplodingAsteroid theExplodingAsteroid;
Asteroid& theAsteroidReference = theExplodingAsteroid;
Visitable<Asteroid>& theSpaceShipReference_1 = theApolloSpacecraft;
theSpaceShipReference_1.accept(theOtherTypeAsteroid);
theSpaceShipReference_1.accept(theAsteroidReference);
AlienA theAlienA;
AlienB theAlienB;
Alien& theAlienReference = theAlienB;
Visitable<Alien>& theSpaceShipReference_2 = theOthersSpacecraft;
theSpaceShipReference_2.accept(theAlienA);
theSpaceShipReference_2.accept(theAlienReference);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment