fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4. using namespace std;
  5.  
  6. class B;
  7. class C;
  8. class A {
  9. public:
  10. virtual void collide(shared_ptr<A> a) =0;
  11. virtual void colisionwith(B&)=0;
  12. virtual void colisionwith(C&)=0;
  13. virtual ~A() {}
  14. };
  15.  
  16. class B : public A {
  17. public:
  18. void collide(shared_ptr<A> a) override { a->colisionwith(*this); }
  19. void colisionwith(B&) override;
  20. void colisionwith(C&) override;
  21. };
  22.  
  23. class C : public A {
  24. public:
  25. void collide(shared_ptr<A> a) override { a->colisionwith(*this); }
  26. void colisionwith(B&) override;
  27. void colisionwith(C&) override;
  28. };
  29.  
  30. void B::colisionwith(C&c) { cout <<"B "<<this<<" colides width a C "<< &c <<endl; }
  31. void B::colisionwith(B&b) { cout <<"B "<<this<<" colides width another B "<< &b <<endl; }
  32. void C::colisionwith(C&c) { cout <<"C "<<this<<" colides width another C "<< &c <<endl; }
  33. void C::colisionwith(B&b) { cout <<"C "<<this<<" colides width a B "<< &b <<endl; }
  34.  
  35. int main() {
  36. vector<shared_ptr<A>> objects;
  37. objects.push_back (make_shared<B>());
  38. objects.push_back (make_shared<B>());
  39. objects.push_back (make_shared<C>());
  40. for (int i = 0; i < objects.size(); i++)
  41. {
  42. for (int j=i+1; j < objects.size(); j++)
  43. {
  44. objects[i]->collide(objects[j]);
  45. }
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
B 0x55744be9bc70 colides width another B 0x55744be9bc30
C 0x55744be9bc50 colides width a B 0x55744be9bc30
C 0x55744be9bc50 colides width a B 0x55744be9bc70