fork download
  1. class Base {
  2. virtual void dummy() = 0;
  3. // this is to generate a vtable, but note there is no virtual f()
  4. };
  5.  
  6. class A : public Base {
  7. public:
  8. void f() { /* ... */ };
  9. void dummy() {};
  10. };
  11.  
  12. class B : public Base {
  13. public:
  14. void f() { /* different implementation from A */ };
  15. void dummy() {};
  16. };
  17.  
  18. template<class T1, class T2, class T3>
  19. void doStuff(T1 &x, T2 &y, T3 &z) {
  20. for (int i=1; i<100000; ++i) {
  21. x.f();
  22. y.f();
  23. z.f();
  24. }
  25. }
  26.  
  27. void doStuff(Base &x, Base &y, Base &z) {
  28. A *a_x = dynamic_cast<A*>(&x);
  29. B *b_x = dynamic_cast<B*>(&x);
  30. A *a_y = dynamic_cast<A*>(&y);
  31. B *b_y = dynamic_cast<B*>(&y);
  32. A *a_z = dynamic_cast<A*>(&z);
  33. B *b_z = dynamic_cast<B*>(&z);
  34. if (a_x && a_y && a_z) {
  35. doStuff(*a_x, *a_y, *a_z);
  36. } else if (a_x && a_y && b_z) {
  37. doStuff(*a_x, *a_y, *b_z);
  38. }
  39. // ... and so on for all eight combinations of A and B.
  40. }
  41.  
  42. int main() {
  43. Base *x = new A();
  44. Base *y = new B();
  45. Base *z = new A();
  46.  
  47. doStuff(*x, *y, *z);
  48. // oops - this instantiates to doStuff(Base &, Base &, Base &)
  49. // and there's no Base::f().
  50. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty