fork(3) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Derived1;
  5. class Derived2;
  6. class Visitor;
  7. class Base
  8. {
  9. public:
  10. virtual void Accept(Visitor &iVisitor, Base& param1) = 0;
  11. virtual void Accept(Visitor &iVisitor, Derived1& param2) = 0;
  12. virtual void Accept(Visitor &iVisitor, Derived2& param2) = 0;
  13. };
  14.  
  15. class Visitor
  16. {
  17. public:
  18. virtual void Visit(Derived1 & param1, Derived1 &param2) { cout << "11\n"; }
  19. virtual void Visit(Derived1 & param1, Derived2 &param2) { cout << "12\n"; }
  20. virtual void Visit(Derived2 & param1, Derived1 &param2) { cout << "21\n"; }
  21. virtual void Visit(Derived2 & param1, Derived2 &param2) { cout << "22\n"; }
  22. };
  23.  
  24. class Derived1: public Base
  25. {
  26. public:
  27. virtual void Accept(Visitor &iVisitor, Base& param1)
  28. { param1.Accept(iVisitor, *this); }
  29. virtual void Accept(Visitor &iVisitor, Derived1& param2)
  30. { iVisitor.Visit(*this, param2); }
  31. virtual void Accept(Visitor &iVisitor, Derived2& param2)
  32. { iVisitor.Visit(*this, param2); }
  33. };
  34. class Derived2: public Base
  35. {
  36. public:
  37. virtual void Accept(Visitor &iVisitor, Base& param1)
  38. { param1.Accept(iVisitor, *this); }
  39. virtual void Accept(Visitor &iVisitor, Derived1& param2)
  40. { iVisitor.Visit(*this, param2); }
  41. virtual void Accept(Visitor &iVisitor, Derived2& param2)
  42. { iVisitor.Visit(*this, param2); }
  43. };
  44.  
  45. void Visit(Visitor& visitor, Base& param1, Base& param2)
  46. {
  47. param2.Accept(visitor, param1);
  48. }
  49.  
  50. int main() {
  51. Derived1 d1;
  52. Derived2 d2;
  53. Visitor v;
  54. Visit(v, d1, d1);
  55. Visit(v, d1, d2);
  56. Visit(v, d2, d1);
  57. Visit(v, d2, d2);
  58. }
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
11
12
21
22