fork download
  1. #include <stdio.h>
  2. class A {
  3. public:
  4. A() : next(0) {
  5. if (head == 0) {
  6. head = this;
  7. } else {
  8. A* step = head;
  9. while (step->next != 0) {
  10. step = step->next;
  11. }
  12. step->next = this;
  13. }
  14. }
  15. virtual ~A() {
  16. if (head == this) {
  17. head = 0;
  18. } else {
  19. A* step = head;
  20. while (step->next != this) {
  21. step = step->next;
  22. }
  23. step->next = next;
  24. }
  25. }
  26.  
  27. static A* head;
  28. A* next;
  29. };
  30.  
  31. class B : public A {
  32. public:
  33. B() {}
  34. virtual ~B() {}
  35. virtual void foo() {
  36. printf("function foo\n");
  37. }
  38. };
  39.  
  40. class C : public A {
  41. public:
  42. C() {}
  43. virtual ~C() {}
  44. virtual void bar() {
  45. printf("function bar\n");
  46. }
  47. };
  48.  
  49. class D : public B {
  50. public:
  51. D() {}
  52. virtual ~D() {}
  53. virtual void foo() {
  54. printf("function foo from D\n");
  55. }
  56. };
  57.  
  58. A* A::head = 0;
  59.  
  60. int main() {
  61. A a_cls;
  62. B b_cls;
  63. C c_cls;
  64. D d_cls;
  65.  
  66. A* step = A::head;
  67. B* step_b = 0;
  68.  
  69. while (step != 0) {
  70. step_b = dynamic_cast<B *>(step);
  71. if (step_b != 0) {
  72. step_b->foo();
  73. }
  74. step = step->next;
  75. }
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
function foo
function foo from D