fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Base {
  6. public:
  7. virtual void func1() {}
  8. virtual void func2() {}
  9. virtual void func3() {}
  10. virtual ~Base() {}
  11. };
  12.  
  13. template<class B> class Derived1 : public B {
  14. public:
  15. void func1() {
  16. cout << "Derived1" << endl;
  17. B::func1();
  18. }
  19. };
  20.  
  21. template<class B> class Derived2 : public B {
  22. public:
  23. void func2() {
  24. cout << "Derived2" << endl;
  25. B::func2();
  26. }
  27. };
  28.  
  29. template<class B> class Derived3 : public B {
  30. public:
  31. void func3() {
  32. cout << "Derived3" << endl;
  33. B::func3();
  34. }
  35. };
  36.  
  37. int main() {
  38. typedef Derived1<Derived2<Base>> Foo;
  39. Base *foo = new Foo;
  40. foo->func1();
  41. foo->func2();
  42. foo->func3(); // do nothing
  43. cout << "---------\n";
  44. typedef Derived2<Derived3<Base>> Qoo;
  45. Base *qoo = new Qoo;
  46. qoo->func1(); // do nothing
  47. qoo->func2();
  48. qoo->func3();
  49. cout << "---------\n";
  50. typedef Derived1<Derived1<Base>> Hoo;
  51. Base *hoo = new Hoo;
  52. hoo->func1(); // output twice
  53. }
  54.  
Success #stdin #stdout 0s 2964KB
stdin
Standard input is empty
stdout
Derived1
Derived2
---------
Derived2
Derived3
---------
Derived1
Derived1